Moschops
Practically a Master Poster
620 posts since Sep 2008
Reputation Points: 258
Solved Threads: 117
at line 42, you can't declare a variable in c in the middle of the program, all variable declarations have to be at the beginning of the code. Also, you can't use a non-constant variable to initialize the size of the array inside brackets. Even if you could do that, you shouldn't have a pointer that points to arrays of characters, you never allocated any memory to that pointer, thats why you are getting the segmentation fault.
chrjs
Junior Poster in Training
96 posts since Feb 2011
Reputation Points: 58
Solved Threads: 19
at line 42, you can't declare a variable in c in the middle of the program
I understand it became legal to do so in C99.
Moschops
Practically a Master Poster
620 posts since Sep 2008
Reputation Points: 258
Solved Threads: 117
And does your compiler support C99?
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
I understand it became legal to do so in C99.
Even though it is legal in C99, its not legal in standard C. C code isn't as portable by writing declarations halfway through the code.
chrjs
Junior Poster in Training
96 posts since Feb 2011
Reputation Points: 58
Solved Threads: 19
Even though it is legal in C99, its not legal in standard C.
In March 2000, ANSI adopted the ISO/IEC 9899:1999 standard. This forum is labelled "Our C forum is the place for Q&A-style discussions related to the C language as per the ANSI C standard."
Do you mean "standard", or do you mean "a previous version"? :)
Moschops
Practically a Master Poster
620 posts since Sep 2008
Reputation Points: 258
Solved Threads: 117
Hmm... I didn't know ANSI C was changed from C90 to C99, I guess I am more than 10 years behind the times...
chrjs
Junior Poster in Training
96 posts since Feb 2011
Reputation Points: 58
Solved Threads: 19
The reason you were got a segmentation fault is because when you declared char *string[len]; in your first code as opposed to char string[len]; like in your second code, there was no memory bing pointed to by string. The first is a pointer to an array of strings, while the second is just a string.
chrjs
Junior Poster in Training
96 posts since Feb 2011
Reputation Points: 58
Solved Threads: 19