c中结构体变量有指针变量

发布于 2025-01-09 23:34:43 字数 1461 浏览 0 评论 0原文

我想知道为什么结构变量传递指针变量来创建内存。

  1. 如果我们执行 box *boxes = malloc(n * sizeof(box)); 会发生什么

  2. 如果我们这样做 box *boxes = malloc(n * sizeof(box));

    然后我们将地址传递给 scanf 函数, 。指针实际上存储的是地址。那为什么我们要传递“&” scanf 到指针?

for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &boxes[i].length, &boxes[i].width, &boxes[i].height);
}
#include <stdio.h>
#include <stdlib.h>
#define MAX_HEIGHT 41

struct box
{
    /**
    * Define three fields of type int: length, width and height
    */
    int length,width,height;
};

typedef struct box box;

int get_volume(box b) {
    /**
    * Return the volume of the box
    */
    return b.length*b.width*b.height;
}

int is_lower_than_max_height(box b) {
    /**
    * Return 1 if the box's height is lower than MAX_HEIGHT and 0 otherwise
    */
    return b.height < 41 ? 1 : 0;
}

int main()
{
    int n;
    scanf("%d", &n);
    box *boxes = malloc(n * sizeof(box));
    for (int i = 0; i < n; i++) {
        scanf("%d%d%d", &boxes[i].length, &boxes[i].width, &boxes[i].height);
    }
    for (int i = 0; i < n; i++) {
        if (is_lower_than_max_height(boxes[i])) {
            printf("%d\n", get_volume(boxes[i]));
        }
    }
    return 0;
}
  • 当我尝试运行 sizeof struct 时,我得到的 sizeof Struct box 是 12。
  • 假设 n = 5,那么内存空间是多少? 内存 = 12 * 5 ?

I want to know why structure variable passes pointer variable to create memory.

  1. what happens if we do box *boxes = malloc(n * sizeof(box));

  2. Then we pass address to scanf function. Pointer actually stores the address. Then why we pass "&" of scanf to pointer ?

for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &boxes[i].length, &boxes[i].width, &boxes[i].height);
}
#include <stdio.h>
#include <stdlib.h>
#define MAX_HEIGHT 41

struct box
{
    /**
    * Define three fields of type int: length, width and height
    */
    int length,width,height;
};

typedef struct box box;

int get_volume(box b) {
    /**
    * Return the volume of the box
    */
    return b.length*b.width*b.height;
}

int is_lower_than_max_height(box b) {
    /**
    * Return 1 if the box's height is lower than MAX_HEIGHT and 0 otherwise
    */
    return b.height < 41 ? 1 : 0;
}

int main()
{
    int n;
    scanf("%d", &n);
    box *boxes = malloc(n * sizeof(box));
    for (int i = 0; i < n; i++) {
        scanf("%d%d%d", &boxes[i].length, &boxes[i].width, &boxes[i].height);
    }
    for (int i = 0; i < n; i++) {
        if (is_lower_than_max_height(boxes[i])) {
            printf("%d\n", get_volume(boxes[i]));
        }
    }
    return 0;
}
  • When I tried running the sizeof struct,I got the sizeof Struct box it was 12.
  • Say n = 5, Then what will be the memory space?
    memory = 12 * 5 ?

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

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

发布评论

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

评论(2

小猫一只 2025-01-16 23:34:43
  1. malloc() 用于分配动态和可变大小的内存。因此,我们使用它来创建一个由 n 结构组成的数组。

  2. 您需要 & 因为 scanf() 需要变量的地址来存储输入数据。 b 是一个指针,但 b[i].length 只是通过取消引用该指针来访问的普通结构成员。您需要获取其地址以传递给 scanf()

  1. malloc() is used to allocate dynamic and variable sized memory. So we use this to create an array of n structures.

  2. You need & because scanf() needs the address of the variable to store the input data to. b is a pointer, but b[i].length is just an ordinary structure member accessed by dereferencing that pointer. You need to get its address to pass to scanf().

木森分化 2025-01-16 23:34:43
 box *boxes = malloc(n * sizeof(box));

为堆上的 n 个 box 实例分配足够的空间,并将

 scanf("%d%d%d", &boxes[i].length///

boxes[i].length 的地址传递给 scanf,并返回指向它的指针。 Scanf 需要在那里写入,因此需要地址。就像

scanf("%d", &n);

您之前所做的

 sizeof(struct box)

一样,等于 12 就可以了。它有 3、4 字节整数。您不会总是简单地理解,编译器可能会填充结构以在 2、4 或 8 字节边界上对齐内容,这就是 sizeof 存在的原因

假设n = 5,那么内存空间是多少?内存 = 12 * 5 ?

是的

 box *boxes = malloc(n * sizeof(box));

Allocates enough space for n instances of box on the heap and returns a pointer to it

 scanf("%d%d%d", &boxes[i].length///

is passing the address of boxes[i].length to scanf. Scanf needs to write there so its needs the address. Its just like

scanf("%d", &n);

that you did earlier

 sizeof(struct box)

equaling 12 is fine. It has 3, 4 byte integers. You wont always get it that simply , the compiler might pad the structure to align things on 2, 4 or 8 byte boundaries, this is why sizeof exists

Say n = 5, Then what will be the memory space? memory = 12 * 5 ?

yes

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