I recently answered a question here in SO where the problem was to input a few sentences of code and print it. My code is this:
#include<stdio.h>
int main() {
clrscr();
int line, i;
char (*string)[100];
printf("How many line?\n");
scanf("%d",&line);
getchar();
for(i=0;i<line;i++) {
gets(string[i]);
}
printf("You entered:\n");
for(i=0;i<line;i++) {
puts(string[i]);
}
return 0;
}
As you see, I haven't allocated any memory to individual strings i.e, s[0]
,s[1]
,....... but surprisingly my compiler didn't give me any warnings or errors for it and it works perfectly.
So I posted this code and frankly got quite a few of downvotes for it (I know I deserved it). Can you please explain why this code works and not give a segmentation error?
My output is as shown: