C 结构体指针初始化

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/AndroidBluetooth/article/details/89578484

原文 C 结构体指针初始化,CSDN同步发布。

转载请注明出处,谢谢!


在使用指针之前,务必要将其初始化。这个是我们最早学习 C 语言的时候,书上经常说的一个问题。在工作中,我们反而会经常忘记这条金科玉律。

本篇文章的所有代码都经 gcc-7 编译器编译过。关于在 macOS 中如何安装和使用 gcc,可以参考 GCC: Homebrew 安装 GCC 和 Binutils 这篇文章。

结构体成员指针的初始化

结构体成员指针的初始化,指的是初始化结构体中指针变量的成员。

我们举个例子,下面是 Animal 的结构体。

struct Animal {
    char *name; //指针成员
    int age;
    char info[200]; //字符数组
    struct Animal *nextAnimal; //指针成员
};

结构体 Animal 含有4个成员变量,其中 nameinfonextAnimal 是指针变量。

写一段测试代码,如下:

int main(int argc, const char *argv[])
{
    struct Animal animal;
    
    printf("animal's name: %s, age: %i, info: %s\n", animal.name, animal.age, animal.info);
    
    return 0;
}

运行结果正常,终端输出如下:

animal's name: (null), age: 0, info: 

我们来验证一下 Animal *nextAnimal 在没有初始化的情况下,会不会有什么问题。

int main(int argc, const char *argv[])
{
    struct Animal animal;
    
    printf("animal's name: %s, age: %i, info: %s\n", animal.name, animal.age, animal.info);
    
    printf("animal.nextAnimal: %p\n", animal.nextAnimal);
    
    printf("animal.nextAnimal->name: %s, age: %i, info: %s\n", animal.nextAnimal->name, animal.nextAnimal->age, animal.nextAnimal->info);
    
    return 0;
}

程序编译没有问题,运行报错

animal's name: (null), age: 0, info: 
animal.nextAnimal: 0x1127fa036
Segmentation fault: 11

修改一下代码,初始化一下 animal.nextAnimal 这个指针,如下:

int main(int argc, const char *argv[])
{
    struct Animal animal;
    
    printf("animal's name: %s, age: %i, info: %s\n", animal.name, animal.age, animal.info);
    
    printf("animal.nextAnimal: %p\n", animal.nextAnimal);
    
    // 初始化指针变量
    animal.nextAnimal = (struct Animal *)malloc(sizeof(struct Animal));
    
    printf("animal.nextAnimal->name: %s, age: %i, info: %s\n", animal.nextAnimal->name, animal.nextAnimal->age, animal.nextAnimal->info);
    
    return 0;
}

再次编译重新运行,还是报错。还需要初始化 animal.nextAnimal->name 这个变量。

int main(int argc, const char *argv[])
{
    struct Animal animal;
    
    printf("animal's name: %s, age: %i, info: %s\n", animal.name, animal.age, animal.info);
    
    printf("animal.nextAnimal: %p\n", animal.nextAnimal);
    
    // 初始化指针变量
    animal.nextAnimal = (struct Animal *)malloc(sizeof(struct Animal));
    
    // 初始化 name 变量
    animal.nextAnimal->name = "cat";
    
    printf("animal.nextAnimal->name: %s, age: %i, info: %s\n", animal.nextAnimal->name, animal.nextAnimal->age, animal.nextAnimal->info);
    
    return 0;
}

编译运行,一切正常。

animal's name: (null), age: 0, info: 
animal.nextAnimal: 0x10f0f1036
animal.nextAnimal->name: cat, age: 0, info: 

通过上面的例子,结构体指针变量有些会给默认值,有些又不会给,所以都要初始化指针变量。修改一下代码,示例如下:

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

struct Animal {
    char *name; //指针成员
    int age;
    char info[200]; //字符数组
    struct Animal *nextAnimal; //指针成员
};

int main(int argc, const char *argv[])
{
    struct Animal animal;
    
    animal.name = "cat";
    strcpy(animal.info, "This is a cat.");
    printf("animal's name: %s, age: %i, info: %s\n", animal.name, animal.age, animal.info);
    
    printf("animal.nextAnimal: %p\n", animal.nextAnimal);
    
    // 初始化指针变量
    animal.nextAnimal = (struct Animal *)malloc(sizeof(struct Animal));
    
    // 初始化变量
    animal.nextAnimal->name = "cat";
    strcpy(animal.nextAnimal->info, "This is a cat.");
    
    printf("animal.nextAnimal->name: %s, age: %i, info: %s\n", animal.nextAnimal->name, animal.nextAnimal->age, animal.nextAnimal->info);
    
    return 0;
}

结构体指针的初始化

指的是初始化结构体指针变量。

int main(int argc, const char *argv[])
{  
    struct Animal *ptAnimal;
        
    printf("ptAnimal's name: %s, age: %i, info: %s\n", ptAnimal->name, ptAnimal->age, ptAnimal->info);
    
    return 0;
}

编译运行报错:

Segmentation fault: 11

同样的道理,需要初始化指针变量。完成后的示例代码如下:

int main(int argc, const char *argv[])
{
    struct Animal *ptAnimal;
    
    // 初始化结构体指针
    ptAnimal = (struct Animal *)malloc(sizeof(struct Animal));
    
    ptAnimal->name = "dog";
    strcpy(ptAnimal->info, "This is a big dog");
    
    printf("ptAnimal's name: %s, age: %i, info: %s\n", ptAnimal->name, ptAnimal->age, ptAnimal->info);
    
    // 初始化结构体指针的成员指针变量 nextAnimal
    ptAnimal->nextAnimal = (struct Animal *)malloc(sizeof(struct Animal));
    ptAnimal->nextAnimal->name = "dog";
    strcpy(ptAnimal->nextAnimal->info, "This is a big dog");
    
    printf("ptAnimal->nextAnimal's name: %s, age: %i, info: %s\n",
           ptAnimal->nextAnimal->name, ptAnimal->nextAnimal->age, ptAnimal->nextAnimal->info);
    
    return 0;
}

完整示例

main.c

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

struct Animal {
    char *name; //指针成员
    int age;
    char info[200]; //字符数组
    struct Animal *nextAnimal; //指针成员
};

int main(int argc, const char *argv[])
{
    /// 验证结构体指针成员变量
    {
        struct Animal animal;
        
        animal.name = "cat";
        strcpy(animal.info, "This is a cat.");
        printf("animal's name: %s, age: %i, info: %s\n", animal.name, animal.age, animal.info);
        
        printf("animal.nextAnimal: %p\n", animal.nextAnimal);
        
        // 初始化指针变量
        animal.nextAnimal = (struct Animal *)malloc(sizeof(struct Animal));
        
        // 初始化变量
        animal.nextAnimal->name = "cat";
        strcpy(animal.nextAnimal->info, "This is a cat.");
        
        printf("animal.nextAnimal->name: %s, age: %i, info: %s\n", animal.nextAnimal->name, animal.nextAnimal->age, animal.nextAnimal->info);
    }
    
    /// 验证结构体指针
    {
        struct Animal *ptAnimal;
        
        // 初始化结构体指针
        ptAnimal = (struct Animal *)malloc(sizeof(struct Animal));
        
        ptAnimal->name = "dog";
        strcpy(ptAnimal->info, "This is a big dog");
        
        printf("ptAnimal's name: %s, age: %i, info: %s\n", ptAnimal->name, ptAnimal->age, ptAnimal->info);
        
        // 初始化结构体指针的成员指针变量 nextAnimal
        ptAnimal->nextAnimal = (struct Animal *)malloc(sizeof(struct Animal));
        ptAnimal->nextAnimal->name = "dog";
        strcpy(ptAnimal->nextAnimal->info, "This is a big dog");
        
        printf("ptAnimal->nextAnimal's name: %s, age: %i, info: %s\n",
               ptAnimal->nextAnimal->name, ptAnimal->nextAnimal->age, ptAnimal->nextAnimal->info);
    }
    
    return 0;
}

编译

gcc-7 main.c -o main

运行

./main

运行结果如下:

animal's name: cat, age: 0, info: This is a cat.
animal.nextAnimal: 0x0
animal.nextAnimal->name: cat, age: 0, info: This is a cat.
ptAnimal's name: dog, age: 0, info: This is a big dog
ptAnimal->nextAnimal's name: dog, age: 0, info: This is a big dog

有事没事都得找朋友叙叙旧,喝喝茶~

展开阅读全文

没有更多推荐了,返回首页