使用 & 时 scanf() 中发生了什么?操作员?

发布于 2024-12-04 03:16:42 字数 866 浏览 1 评论 0原文

我是 C 编程新手,对以下几行代码有疑问。这是在创建结构体 film 的链接列表的上下文中发生的:

struct film {
    char title[TSIZE];
    int rating;
    struct film * next;
}

int main(void)
{
    struct film * head = NULL;
    struct film * prev, *current;
    char input[TSIZE]; 

    // some code ommitted

    strcopy(current->title, input);

    puts("Enter your rating <0-10>");

    scanf("%d", &current->rating);
}

基本上我的问题是关于 strcopy() 和 scanf() 函数。我注意到,对于 strcopy,第一个参数使用成员访问运算符 ->在指向结构的指针上。我相信 strcopy 的第一个参数应该是指向 char 的指针,因此当使用成员访问运算符时,即使 title 没有声明为结构内的指针,我们是否会获得指向 title 的直接指针?

我很困惑这与我们使用 & 的 scanf() 调用有何不同。运算符获取当前->评级的地址。 scanf() 是获取结构指针的地址然后进行成员访问还是结构成员“评级”的地址?为什么不像 strcopy() 那样直接传入指针呢?

我想,&current-> rating 与 &(current-> rating) 之间有区别吗? &current->Rating 是一个指针的地址(有点像指向指针的指针?)。

提前致谢。

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;
    struct 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.

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

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

发布评论

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

评论(3

倒数 2024-12-11 03:16:42

当您在 C 中将数组作为函数参数传递时,编译器实际上传递了一个指向数组中第一个元素的指针。 arrayVar&arrayVar[0] 相同。因此,strcopy 函数的第一个参数是指向 current 指向的结构中字符数组 title 的第一个元素的指针。

当您将 int 传递给函数时,您只是传递变量的值,而不是指向它的指针。由于 scanf 需要一个用于存储值的指针,因此您必须使用 & 来获取指向变量的指针。 scanf 的第二个参数是指向 current 指向的结构中的整数 rating 的指针。

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.

不回头走下去 2024-12-11 03:16:42

即使 title 没有在结构体中声明为指针,我们是否会获得指向 title 的直接指针?

要完全掌握这个问题的答案,您需要了解数据如何存储在内存中。

基本上(为了简单起见),计算机有成排的内存块,每个部分都有一个地址(就像一排房子)。单个变量(如 char A;)只有一处,因此是静态地址。变量数组(如 char Arr[10])有多个房屋(全部排成一行),但数组(指针)本身一次只能指向一个房屋。

有点像使用磁带。因此,当您说 Array[1] 时,您实际上是在说“相对于第一个房子 [0],向下移动到下一个房子 [1]”(或者更技术地说,内存地址加上指针的大小乘以如何您想要移动过去的许多街区)。

scanf() 是获取结构指针的地址然后进行成员访问还是结构成员“评级”的地址?

它是结构体成员评级的地址。通常为了澄清取消引用,人们会使用括号,因此 &(current-> rating),而不是传递 current (¤t),但通常编译器知道正在取消引用的内容,尽管如果出现问题,括号可以提供帮助。

为什么不像strcopy()那样直接传入指针呢?

从技术上讲,该数组是一个指针(多个房屋之类的东西),而评级没有指针(一栋房屋,不需要)。如果你有一个整数数组,那么你就可以做到这一点。

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.

哆兒滾 2024-12-11 03:16:42

current->title 已经是一个指针(类型为 char[] ),因此不需要 & 运算符 while current-> rating 不是指针(类型是 int),因此需要获取变量的地址(这就是 & 运算符的地址)在这里做)。

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

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