If ListofChar is a pointer to Char (do not confuse with char) which represents struct charact,
then what does it mean the ListofChar chars in void report(ListofChar chars)?
Does chars just represents the pointer ListofChar?
#include <stdio.h>
#include <stlib.h>
struct charact {
char ch;
int occurs;
struct charact *next;
};
typedef struct charact Char;
typedef Char * ListofChar;
typedef Char * CharNode_ptr;
void letters(char name[50], ListofChar * chars_ptr);
void report(ListofChar chars);
Char * createnode(char ch);
int main() {
char name[50];
ListofChar chars = NULL;
scanf("%s", name);
letters(name, &chars);
report(chars);
return 0;
}
Char * createnode(char ch) {
CharNode_ptr newnode_ptr ;
newnode_ptr = malloc(sizeof (Char));
newnode_ptr -> ch = ch;
newnode_ptr -> occurs = 0;
newnode_ptr -> next = NULL;
return newnode_ptr;
}
Yes and no.
It seems that the code is written to imply the difference of "one charact" and "many pieces of charact".
Something like:
Char is one, a single struct.
ListOfChar in itself is a pointer to a single Char, but it implies that there are more of it following. With some way of identifying the last one, this allows a function to work on several, with only one parameter. Even without knowing immediatly how many there are.
There is another similar construct, which you do not mention in your question and which is apart from declaration not used in the shown code, CharNode_ptr. If you allow some pure speculation, I'd guess that the "more following" from above, is implemented in the rest of the code as a linked list.
The implication here is slightly different (or just more detailed), it suggests the additional detail that you find the "other" characts via following the link next inside each struct, provided that it is not NULL.
It is likely that the code is written to NOT imply this detail when using the ListOfChar type identifier, e.g. in the API (shown prototypes) for functions.
Something like:
ListofChar means:
"Hey user, do not worry, it is somehow more than one; but you do not need to know how exactly."
CharNode_ptr means:
"Hey programmer, keep in mind that you are dealing with linked lists."
This answer is based on my understanding of your question, which is as follows, I think your problem lies with the declaration of an array using pointers and a pointer.
As you know, every variable is a memory location and every memory location has its address defined. A pointer of any given type contains the address to the memory location. For your case, consider the following example.
struct charact * a;
we define a pointer 'a' of type 'struct charact' as of now the pointer 'a' holds a garbage value and if you try to assign something to it will give you an error because when you assign you system goes to 'a' takes up the garbage value and think of it as a memory address where it has to put the given value which cannot be done because the garbage address is probably not assigned to you. In order to assign a value at 'a' you first have to initialise 'a' by using malloc() or any other memory allocation functions. On doing so malloc() would return a memory address to you and then you can start and assign values to 'a'.
Now coming to your question, defining 'a' can act as an array or as a pointer to the structure. This depends on how much memory you are assigning to the variable using malloc(). You would probably assign the value as follows -
a = (struct charact *)malloc(sizeof(struct charact)*n)
if n>1, then you will get a memory address which is capable of holding more then on struct charact and hence will work as an array, and, if n=1 you can hold only one struct charact and hence it is a single entity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With