I have a small problem with a program that when running, sends me a segmentation violation apparently occurs in the function "ejer6" when trying to access the position [0]
of char * mensaje
to assign 9
my code :
#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;
}