我想知道我在这里做错了什么?我将 void 指针转换为结构缓冲区,它只打印出垃圾。现在 buffer 不应该指向 ptr ,它是一个指向我们为其分配内存的原始缓冲区的指针?
#include <stdio.h>
#include <pthread.h>
struct buffer{
int a;
char *string[];
}buffer;
void thread1_function(void *ptr){
struct buffer *buffer=(struct buffer*)ptr;
printf("hello world\n");
printf("%s-%n\n", buffer->string,buffer->a);
}
int main(){
struct buffer *buffer;
int err;
buffer = (struct buffer*)malloc((11*sizeof(char))+sizeof(int));
pthread_t thread1;
sprintf(buffer->string,"%s","strint");
buffer->a=1;
printf("main: %s - %d\n",buffer->string,buffer->a);
err = pthread_create(&thread1, NULL, thread1_function, &buffer);
printf("error: %d\n",err);
pthread_join(thread1,NULL);
return 0;
}
~