I have a char* within a "news" struct like this:
typedef struct news{
char *name;
}news;
I read from a file some text (in example I have ever the same line:hello). For the main and my function i wrote this code:
int insert(news **note, char *text,int i);
int main(){
news *note;
int i=0,j;
note = malloc(sizeof(news));
for (j=0;j<5;j++){
i=insert(¬,"hello",i);
printf("%s\n",note[i-1].name);
}
system("pause");
}
int insert(news **note, char *text,int i){
(*note)[i].name = malloc(strlen(text)*sizeof(char));
strcpy((*note)[i].name,text);
note = realloc((*note),++i*sizeof(news));
return i;
}
Why do I get a segmentation fault for j>2
? What is wrong?