C语言中如何定义结构体指针

2026-02-15 19:49:24

1、打开啊哈c编译器

C语言中如何定义结构体指针

2、struct student

{

    int score;

    char name[128];

};//先定义一个结构体

C语言中如何定义结构体指针

3、struct student stul;

    stul.score = 100;

    strcpy(stul.name,"张三");

    printf("名字:%s,分数:%d\n",stul.name,stul.score);

C语言中如何定义结构体指针

4、struct student *p;//野指针

    //p.score = 100;//如果用结构体指针,就不能用点运算访问结构体中的变量,要用->

                    //指针要注意是否是野指针或者NULL

    p = (struct student *)malloc(sizeof(struct student));//开辟空间

    memset(p,'\0',128);

C语言中如何定义结构体指针

5、 p->score = 98;

    strcpy(p->name,"李四");

   printf("名字:%s,分数:%d\n",p->name,p->score);

C语言中如何定义结构体指针

6、printf("地址是:%p\n",p++);

   printf("加后地址是:%p\n",p);    

C语言中如何定义结构体指针

7、运行程序

C语言中如何定义结构体指针

猜你喜欢