开发者

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

I'm using xCode because I found the debugger to be very useful. So in the debugger I see, after I enter the students name name[0] = \0 no matter what. But then the rest of the name will be correct. For example, if I put John, it will come back saying \0, o, h, n. Help 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);
        s开发者_Python百科trcpy(studentArray[empty].department, department);
        studentArray[empty].rank = rank;


Do this:

    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 into pointers to the first elements of the respective arrays (&name[0], &department[0]) when they are used in an expression (there are expections to this rule, see here for details).

Read this for further reference.


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

scanf needs a memory address to write to. In case of a string it expects a char * and passing a char[] is fine in this case (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 already did.


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

scanf("%d", &rank);

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

scanf("%s", &name);

you want:

scanf("%s", name);

And similarly for other arrays.


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 (such as scanf()) calls for a char * it wants the pointer to the first element.

scanf("%s", name);


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

When you try to pass the address of the varible (&name), you are passing a pointer to that variable's memory location (in your case, this decays to char(*)[0]). This question intrigued me because name and &name will have the same value, but different types. Since printf/scanf are defined to receive char*, passing char(*)[] results in undefined behavior - some compilers will handle if for you while others won't (VS2010 and maybe earlier versions handle this for you).

I was close, but had some help with this answer (learned a lot): & operator definition for arrays in C. If they provide an answer here, I'll delete mine.


use scanf("%s", name);

avoid using &, it's too easy to get wrong.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜