c中结构体变量有指针变量
我想知道为什么结构变量传递指针变量来创建内存。
如果我们执行
box *boxes = malloc(n * sizeof(box)); 会发生什么
- 如果我们这样做
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.
what happens if we do
box *boxes = malloc(n * sizeof(box));
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
malloc()
用于分配动态和可变大小的内存。因此,我们使用它来创建一个由n
结构组成的数组。您需要
&
因为scanf()
需要变量的地址来存储输入数据。b
是一个指针,但b[i].length
只是通过取消引用该指针来访问的普通结构成员。您需要获取其地址以传递给scanf()
。malloc()
is used to allocate dynamic and variable sized memory. So we use this to create an array ofn
structures.You need
&
becausescanf()
needs the address of the variable to store the input data to.b
is a pointer, butb[i].length
is just an ordinary structure member accessed by dereferencing that pointer. You need to get its address to pass toscanf()
.为堆上的 n 个 box 实例分配足够的空间,并将
boxes[i].length 的地址传递给 scanf,并返回指向它的指针。 Scanf 需要在那里写入,因此需要地址。就像
您之前所做的
一样,等于 12 就可以了。它有 3、4 字节整数。您不会总是简单地理解,编译器可能会填充结构以在 2、4 或 8 字节边界上对齐内容,这就是
sizeof
存在的原因是的
Allocates enough space for n instances of box on the heap and returns a pointer to it
is passing the address of
boxes[i].length
to scanf. Scanf needs to write there so its needs the address. Its just likethat you did earlier
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
existsyes