I encountered a segmentation fault when trying to print an element of an array of c-style strings (at the final iteration i=3). However, I am able to print the same element outside the for-loop.
char* pipe_segments[2];
pipe_segments[0] = "ls";
pipe_segments[1] = "-l";
pipe_segments[2] = "-h";
printf("%s\n", pipe_segments[0]);
printf("%s\n", pipe_segments[1]);
printf("%s\n", pipe_segments[2]);
for(int i = 0; i < 3; i++) {
printf("%s\n", pipe_segments[i]);
}
Here is the output
ls
-l
-h
ls
-l
Segmentation fault
Why is there a segmentation fault only in the for-loop?