I am new to C programming, and have a question about the following couple lines of code. This takes place within the context of a creating a linked list of struct film:
struct film {
char title[TSIZE];
int rating;
struct film * next;
}
int main(void)
{
struct film * head = NULL;
struct film * prev, *current;
char input[TSIZE];
// some code ommitted
strcopy(current->title, input);
puts("Enter your rating <0-10>");
scanf("%d", ¤t->rating);
}
Basically my question is about the strcopy() and scanf() functions. I notice that with strcopy the first parameter is using the member access operator -> on a pointer to the struct. I believe that the first argument to strcopy is supposed to be a pointer to char, so when using the member access operator, are we getting a direct pointer to title even though title is not declared as a pointer inside the struct?
I am confused about how this contrasts with the scanf() call where we use the & operator to get the address of current->rating. Is scanf() taking the address of the struct pointer then doing member access or is it the address of the structs member ‘rating’? Why not just pass in the pointer similarly to strcopy()?
Id imagine there is a difference between doing ¤t->rating vs &(current->rating)? Is ¤t->rating the address of a pointer (kind of like a pointer to pointer?).
Thanks in advance.
When you pass an array as a function argument in C, the compiler actually passes a pointer to the first element in the array.
arrayVaris the same as&arrayVar[0]. So, thestrcopyfunction’s first argument is a pointer to the first element of the character arraytitlein the structure pointed to bycurrent.When you pass an
intto a function, you are simply passing the value of the variable, not a pointer to it. Sincescanfrequires a pointer that it will store the value in, you have to use&to get a pointer to the variable instead. The second argument toscanfis a pointer to the integerratingin the structure pointed to bycurrent.