gpt4 book ai didi

c - 修改字符串时出现段冲突

转载 作者:太空宇宙 更新时间:2023-11-03 23:48:33 24 4
gpt4 key购买 nike

<分区>

我有一个程序的小问题,在运行时,当试图访问 char * 的 position [0] 时,函数“ejer6”显然发生了分段违规mensaje 分配 9 我的代码:

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

struct structure {

char * message;
int integer;
};

void * ejer6(void * arg) {

printf("Enters within");

struct structure * turas;
turas=((struct structure *)arg;

turas->integer=turas->integer+1;
turas->message[0]='9';

printf("%d\n", turas->integer);
printf("%s\n", turas->message);


pthread_exit(NULL);
}

int main(int argc, char ** argv) {
if(argc!=2) {
printf("Enter the appropriate number of parameters\n");
}
else {

pthread_t h[argc];
struct structure turas[argc];
int i;

for(i=0; i<argc; i++) {
turas[i].integer=13;
turas[i].message="hello world";
}

for(i=0; i<argc; i++) {

printf("\nThis is the value of the structure before the strand: \n");
printf("Integer: %d \n", turas[i].integer);
printf("Message: %s \n", turas[i].message);


pthread_create(&h[i], NULL, ejer6, (void *)&turas[i]);
pthread_join(h[i], NULL);

printf("\nThis is the value of the structure after the strand: \n");
printf("Integer: %d \n", turas[i].integer);
printf("Message: %s \n", turas[i].message);

}
}

return 0;
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com