Scanf () (C language) confused me

When do I need to insert / not insert & for scanf() in C? Thanks.

 int main() { char s1[81], s2[81], s3[81]; scanf("%s%s%s", s1, s2, s3); // If replace scanf() with the expression below, it works too. // scanf("%s%s%s", &s1, &s2, &s3); printf("\ns1 = %s\ns2 = %s\ns3 = %s", s1, s2, s3); return 0; } //programming is fun // //s1 = programming //s2 = is //s3 = fun 
+6
c scanf
source share
9 answers

scanf places the scanned values ​​at the address specified by the arguments. The address operator is an address and is used to determine the address of a variable.

But you use arrays, and arrays are reduced to pointers when using functions as arguments. Thus, you do not need to use the operator in the case of arrays either.

Example:

 char s[81]; int n; int* nptr; //Initialize nptr to some meaningful value scanf("%s %d %d",s,&n,nptr); 

In this case, we need to use the and operator to get the address n. We do not need to use it with nptr, because it is already a pointer to some place in memory, nor with s, because the array goes to the pointer when passing to the function.

+9
source share

Arguments after the format specifier must be pointers. When the array name is passed to the function, the location of the source element is passed, so you don't need to use & at all in your example. You could do it though (from K & R ):

 int day, year; char monthname[20]; scanf("%d %s %d", &day, monthname, &year); 

Since day and year are int , you must use & to get the address of these variables. Since monthname is an array, no & is required.

+6
source share

In C, when you are dealing with arrays:

 int c_array[24]; c_array == &c_array == &c_array[0] 

Yes, it’s funny - they look the same (difference in types). Read this one .

+4
source share

From comp.lang.c FAQ: I thought you always needed & for every variable passed to scanf .

I highly recommend reading the rest of the comp.lang.c FAQ .

+3
source share

s1 returns the address of the first element of the array, and &s1 returns the address of the array itself. The address of the first element and the address of the array itself are identical, which is why both views work.

+2
source share

Just a guess, but I would suggest that s1 , s2 and s3 are arrays.

+1
source share

If a is an array, then both a and &a lead to a pointer in this context:

  • a is due to the decomposition of the array into a pointer (C99, Β§6.3.2.1 / 3):

    Unless it is an operand of the sizeof or unary operator and the operator , or is a string literal used to initialize an array, an expression of type '' an array of type is converted to an expression of type '' with a pointer to a type pointing to the starting element of the array object and is not an lvalue.

    ... added emphasis

  • &a executes as a result of the & operator - returns a pointer to an object.

+1
source share

Scanf accepts a variable number of parameters. The first argument to scanf is the format and after that n addresses, where the values ​​to be stored, while n is the number of formats specified in the format string. since you are using an array to store the values ​​you have to provide the base address of the array.

 // If int
 int i;
 scanf ("% d", & i);  // store int value at address & i

 float f;
 scanf ("% f", & f);  // store float value at address & f

 int a [10];
 scanf ("% s", a);  // store string or array value at a

if using the last line confuses you, you can try the same thing with the address of the first element, which is & a [0].

Hope this helps,
Gg

+1
source share

Typically, you will use scanf the way you do in your relaxed string. How C arrays work, the name of arrays, such as s1, s2, and s3, is actually the address of the first element of the array. However, for any primitives, you will need the syntax & variable. For example:

 float f; scanf("%f", &f); 
0
source share

All Articles