编写我的第一个 C 程序,但我无法克服这个愚蠢的错误

发布于 2024-11-04 03:07:16 字数 684 浏览 0 评论 0原文

我使用 xCode 是因为我发现调试器非常有用。所以在调试器中我看到,在我输入学生姓名 name[0] = \0 后,无论如何。但名称的其余部分将是正确的。例如,如果我输入 John,它会返回 \0、o、h、n。请帮忙?

        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;

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);
        strcpy(studentArray[empty].department, department);
        studentArray[empty].rank = rank;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

悍妇囚夫 2024-11-11 03:07:16

执行此操作:

    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);

请注意对 scanf 的前两次调用中没有 & 符号。这是因为编译器隐式地将 namedepartment 转换为指向各自数组第一个元素的指针 (&name[0], < code>&department[0]) 当它们在表达式中使用时(对此规则有期望,请参阅 此处了解详细信息)。

请阅读本文以获取更多参考。

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.

南…巷孤猫 2024-11-11 03:07:16

您需要使用 scanf("%s", name); 而不是 scanf("%s", &name); (与 department< /代码>)。

scanf 需要一个要写入的内存地址。对于字符串,它需要一个 char * 并传递一个 char[] 在这种情况下就可以了(但它很容易受到缓冲区溢出的影响!)。

但是,对于您的整数(不是指针),您需要传递整数的内存地址,即&rank而不是rank > - 你已经这样做了。

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.

凡尘雨 2024-11-11 03:07:16

scanf 函数需要知道它需要读入的内存中的地址,因此对于整数之类的东西

scanf("%d", &rank);

是正确的。但是像 name 这样的东西已经是地址(数组的名称实际上是其第一个元素的地址),所以

scanf("%s", &name);

你想要的不是 :

scanf("%s", name);

,对于其他数组也类似。

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.

淡紫姑娘! 2024-11-11 03:07:16

当你声明一个数组时;

    char name[MAX_NAME_LENGTH];

没有索引的name是指向数组中第一个元素的指针。

name == &name[0]

当函数(例如 scanf())调用 char * 时,它需要指向第一个元素的指针。

scanf("%s", name);

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);
甜妞爱困 2024-11-11 03:07:16

您的变量 namedepartment 是字符数组 (char (*)[])。当您将这些变量传递给 printf/scanf (或任何其他函数,数组通过引用传递)时,它们的类型衰减为 TYPE*​​ (在您的情况下 char* )并且作为数组中第一个元素的内存位置传递。

当您尝试传递变量的地址 (&name) 时,您将传递一个指向该变量的内存位置的指针(在您的情况下,这会衰减为 char(*)[0 ])。这个问题引起了我的兴趣,因为 name&name 将具有相同的值,但类型不同。由于 printf/scanf 被定义为接收 char*,传递 char(*)[] 会导致未定义的行为 - 一些编译器会为您处理 if,而其他编译器则不会(VS2010 和早期版本可能会处理此问题)为你)。

我很接近,但对这个答案有一些帮助(学到了很多): & C 中数组的运算符定义。如果他们在这里提供答案,我会删除我的。

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.

oО清风挽发oО 2024-11-11 03:07:16

使用 scanf("%s", name);

避免使用 &,太容易出错。

use scanf("%s", name);

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文