What is happening in scanf() when using the & operator?
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;
struc开发者_StackOverflowt 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. arrayVar
is the same as &arrayVar[0]
. So, the strcopy
function's first argument is a pointer to the first element of the character array title
in the structure pointed to by current
.
When you pass an int
to a function, you are simply passing the value of the variable, not a pointer to it. Since scanf
requires a pointer that it will store the value in, you have to use &
to get a pointer to the variable instead. The second argument to scanf
is a pointer to the integer rating
in the structure pointed to by current
.
are we getting a direct pointer to title even though title is not declared as a pointer inside the struct?
To grasp the answer to this question fully, you need to understand how data is stored in memory.
Basically (to keep this simple), the computer has rows of memory blocks, each part has an address (like a row of houses). A single variable (like char A;) has only one house and is thus a static address. An array of variables (like char Arr[10]) has multiple houses (all in a row) but the array (pointer) itself can only point to one house at a time.
A bit like using a tape. So when you say Array[1] you're really saying 'relative to the first house [0], move down to the next house [1]' (or more technically, the memory address plus the size of the pointer times by how many blocks you want to move past).
Is scanf() taking the address of the struct pointer then doing member access or is it the address of the structs member 'rating'?
It's the address of struct's member, rating. Normally to clarify dereferencing, one would use brackets, so &(current->rating), as opposed to passing current (¤t), but generally compilers know what is being dereferenced, although bracketing can help if there's an issue.
Why not just pass in the pointer similarly to strcopy()?
The array techically is a pointer (multiple houses sorta thing), where-as rating has no pointer (one house, not needed). If you had an array of ints then you'd be able to do that.
current->title
is already a pointer (the type is char[]
), so you don't need the &
operator while current->rating
is not a pointer (the type is an int), so you need to get the address of the variable (which is what the &
operator is doing here).
精彩评论