绝代码农 发表于 2021-8-7 13:09:16

c线程传递多个参数,使用结构体

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

#include "header-demo.h"

void display(void *couple);

typedef struct Persons {
    int age;
    char *husband;
    char *wife;
} Couple;

int main() {
    pthread_t pthread;
    Couple *couple = (Couple *)alloca(sizeof(Couple));
    couple->age = 10;
    couple->wife = "zhu";
    couple->husband = "guan";
//    strcpy(couple->husband, "guan");
//    strcpy(couple->wife, "zhu");
    pthread_create(&pthread, NULL, display, (void *)couple);
    pthread_join(&pthread, NULL);

    return 0;
}

void display(void *couple) {
    Couple *couple1 = (Couple *)couple;
    printf("age:%d, name1: %s, name2:%s\n", couple1->age, couple1->husband, couple1->wife);
}



文档来源:51CTO技术博客https://blog.51cto.com/u_2498536/3304161
页: [1]
查看完整版本: c线程传递多个参数,使用结构体