for (int i = 0; i <12; i++) {
cin>>buf;
strcpy(str_ex[i].cstr,buf) ;
}
system("PAUSE");
return 0;
}
if i run this code, when i=0, everything is fine. But when it enters the loop second time i.e when i = 1, i get a segmentation fault error. I am not able to understand this behaviour. When I change the definition of cstr to an array, everything is fine. but when cstr is a pointer to char, as in the above case, then I get the problem.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
why I am getting segmentation fault error during the second cycle and not during the first.
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
int main()
{
struct str_misc {
char* cstr;
};
str_misc str_ex[12];
char buf[12];
for (int i = 0; i <12; i++) {
cin>>buf;
strcpy(str_ex[i].cstr,buf) ;
}
system("PAUSE");
return 0;
}
if i run this code, when i=0, everything is fine. But when it enters the loop second time i.e when i = 1, i get a segmentation fault error. I am not able to understand this behaviour. When I change the definition of cstr to an array, everything is fine. but when cstr is a pointer to char, as in the above case, then I get the problem.
For a programming question, it is probably better to post to the other forum (Bloodshed Forum) rather than the developers forum....
Wayne