struct的申明,声明为指针与变量的区别

来源:互联网 发布:ad10电路设计软件 编辑:程序博客网 时间:2024/05/21 08:02


#include "stdio.h"
#include "stdlib.h"
struct ssd_ch_lun_alloc{
int start_lun;
int end_lun;
};




int main(void)
{
struct ssd_ch_lun_alloc alloc[12];//数组变量,已经赋值的情况下, 在栈中
struct ssd_ch_lun_alloc *test_alloc;//指针在并赋值的情况下使用了
test_alloc = (struct ssd_ch_lun_alloc *)malloc(sizeof(struct ssd_ch_lun_alloc));//指针赋值, 在堆中
alloc[0].start_lun = 0;
alloc[0].end_lun = 1;
test_alloc->start_lun = 2;
test_alloc->end_lun = 4;
printf("The number is %d, %d\n", alloc[0].start_lun, alloc[0].end_lun);
return 1;

}


声明为变量时,并分配内存空间,在进行struct中的变量赋值时,可直接使用。

声明为指针时,并未进行赋值,也没有分配内存空间。需要用malloc进行内存空间的分配,并将内存起始地址返回给指针变量

原创粉丝点击