I study electronics engineering and I'm learning C as my first language. I've been following IIT C Programming course, and it often takes me a little more time to understand the program when pointers are involved.
I'm learning about linked lists right now, I'll put part of the code then tell you guys what I'm having problem with. I'll not put everything because I don't think it will be necessary.
typedef struct node_type {
int data;
struct node_type *next;
} node;
typedef node_type *list;
list head, temp;
char ch;
int n;
head = NULL;
scanf("%c", &ch);
while (ch == 'Y' || ch == 'y') {
temp = (list)malloc(sizeof(node));
temp->data = n;
temp->next = head;
head = temp;
scanf("%c", &ch);
I couldn't understand why the use of '->' when working with pointers to structs, I know now that temp->data = n;
is equivalent to (*temp).data = n;
. But I'm having problem with the syntax of the second expression and why using it gives access to 'data' inside the struct which 'temp' points to.
When declaring variables there's an order as seen here: http://ieng9.ucsd.edu/~cs30x/rt_lt.rule.html
I know that there's a precedence order for the operators. In declarations I read * as "pointer to". How should I read (*temp).data
in the middle of the code (not declaration) ?
In the beginning of the course, Dr.P.P.Chakraborty says that * could be understand as "the content of", but it wouldn't make sense in this expression: "the content of temp..."
- What is the meaning of the operator * when not used in declarations?
- The use of
typedef
follows the same rule of declarations likeint *a[]
?typedef int *a[]
declares that the type 'a' is an array of pointers to int?
Appreciate any help.