二级指针的例子,用于理解 pthread_join 的参数

通过参数返回值

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

void get_string(char **pp)
{
    char *p = "hello";
    *pp = p;
}

struct person {
    char name[8];
    int age;
};

void get_person(struct person **pp)
{
    struct person *p = malloc(sizeof(struct person));
    strcpy(p->name, "tom");
    p->age = 10;
    *pp = p;
}

int main()
{
    char *s;
    get_string(&s);
    printf("Get string: %s\n", s);

    struct person *tom;
    get_person(&tom);
    printf("name = %s, age = %d\n", tom->name, tom->age);
    return 0;
}

程序输出

Get string: hello
name = tom, age = 10