C语言 传指针和传地址的区别

#include <stdio.h>
#include <stdlib.h>

struct students{
    int age;
    int high;
    int *p;
};

void init(struct students* s )
{
    s->age=10;
    s->high=188;
    s->p=(int*)malloc(sizeof(int*));
}

int main()
{
    struct students* tom;
    init(tom);
    printf("tom.age=%d,tome.high=%d,tom.p=%p\n",tom->age,tom->high,tom->p);
   
    return 0;
}

编译代码会出现段错误!!!

代码修改如下:

#include <stdio.h>
#include <stdlib.h>

struct students{
    int age;
    int high;
    int *p;
};

void init(struct students* s )
{
    s->age=10;
    s->high=188;
    s->p=(int*)malloc(sizeof(int*));
}

int main()
{
    struct students tom;
    init(&tom);
    printf("tom.age=%d,tome.high=%d,tom->p=%p\n",tom.age,tom.high,tom.p);
   
    return 0;
}

编译正常!!!

 

上一篇:Postman如何做接口测试4:如何自动添加请求头


下一篇:HDFS的一些配置文件修改