Writing my first C program and I can't get past this dumb mistake

I use xCode because I found the debugger very useful. Therefore, in the debugger that I see, after entering the student name [0] = \ 0, it does not matter. But then the rest of the name will be correct. For example, if I put John, he will return, saying \ 0, o, h, n. Help me please?

char name[MAX_NAME_LENGTH]; char department[MAX_DEPT_LENGTH]; int rank; int empty = 0; printf("\nPlease enter the students name: "); scanf("%s", &name); printf("\nPlease enter the students department: "); scanf("%s", &department); printf("\nPlease enter the students rank: "); scanf("%d", &rank); strcpy(studentArray[empty].name, name); strcpy(studentArray[empty].department, department); studentArray[empty].rank = rank; 
+2
source share
6 answers

Do it:

  printf("\nPlease enter the students name: "); scanf("%s", name); printf("\nPlease enter the students department: "); scanf("%s", department); printf("\nPlease enter the students rank: "); scanf("%d", &rank); 

Note the absence of ampersands in the first two calls to scanf . This is because the compiler implicitly converts name and department to pointers to the first elements of the corresponding arrays ( &name[0] , &department[0] ) when they are used in the expression (there are expectations for this rule, see here ).

Read this for more details.

+2
source

You need to use scanf("%s", name); instead of scanf("%s", &name); (same for department ).

scanf needs a memory address for writing. In the case of a string, he expects char * and passing char[] in this case is fine ( it is vulnerable to buffer overflows, though! ).

However, for your integer - which is not a pointer - you need to pass the memory address of the integer, i.e. &rank instead of rank - which you have already done.

+1
source

Your name and department variables are character arrays ( char (*)[] ). When you pass these variables to printf / scanf (or any other function, the arrays are passed by reference), their types decompose into TYPE* (in your case char* ) and are passed as a memory cell of the first element to the array.

When you try to pass the address of a variable ( &name ), you pass a pointer to this variable memory cell (in your case, it splits into char(*)[0] ). This question intrigued me because name and &name will have the same value, but different types. Since printf / scanf is defined to accept char* , passing char (*) [] leads to undefined behavior - some compilers will process if for you, while others will not (VS2010 and, possibly, earlier versions handle this for you).

I was close, but helped a bit with this answer (learned a lot): & operator definition for arrays in C. If they give an answer here, I will delete mine.

0
source

The scanf function must know the address in memory that needs to be read, so for things like integers

 scanf("%d", &rank); 

is correct. But things like name are already addresses (the name of the array is actually the address of its first element), so instead of:

 scanf("%s", &name); 

Do you want to:

 scanf("%s", name); 

And similarly for other arrays.

0
source

When you declare an array,

  char name[MAX_NAME_LENGTH]; 

name without an index is a pointer to the first element in the array.

 name == &name[0] 

When a function (e.g. scanf() ) calls char * , it wants a pointer to the first element.

 scanf("%s", name); 
0
source

use scanf("%s", name);

avoid using &, too easy to make a mistake.

-1
source

All Articles