You have the parameters the wrong way round here. You're splitting the string in phrase which is uninitialized, and then you're assigning the result to word thus overwriting the pointer to the memory you previously allocated.
Up vote 2 down vote favorite share g+ share fb share tw.
I keep getting that error. I am pretty sure it has something to do with memory allocation but i'm not quite sure how to fix it. #include #include #include #include char * VOWELS ="aeiouAEIOU"; void printLatinWord(char *a); int main(int argc, char **argv){ char phrase100; char *word = malloc(sizeof(char) *100); printf("Enter the phrase to be translated: \n"); fgets(word, 100, stdin); printf("The phrase in Pig Latin is:\n"); word = strtok(phrase, " "); printLatinWord(word); return 0; } void printLatinWord(char *word){ while (strchr(VOWELS, *word) == NULL){ char consonant = *word; char *to=word, *from=word+1; while (*from) *to++=*from++; *to=consonant; } printf("%say\n", word); } the out put gives "Segmentation fault (core dumped)" c segmentation-fault link|improve this question edited Feb 3 at 6:15Timothy Jones4,394825 asked Nov 11 '11 at 16:36livelaughlove1016 89% accept rate.
While this isn't an answer, you should really learn how to use gdb as it will allow you to see exactly where the problem occurs. – Michael Mior Nov 11 '11 at 16:37 How far does it get before it seg faults? Do the other printf statements get executed?
Might want to use a debugger to step through. – birryree Nov 11 '11 at 16:37 This looks a bit spurious: char *word = malloc(sizeof(char) *100);. You then reassign word with the return value of strtok().
– trojanfoe Nov 11 '11 at 16:40.
Fgets(word, 100, stdin); word = strtok(phrase, " "); You have the parameters the wrong way round here. You're splitting the string in phrase which is uninitialized, and then you're assigning the result to word thus overwriting the pointer to the memory you previously allocated. You probably intended for the fgets to read the input into phrase rather than word.
Ty! That fixed it! – livelaughlove Nov 11 '11 at 16:46 1 Note that you don't need to allocate memory for word.
After calling strtok it will point to the data inside the phrase buffer. – Graham Borland Nov 11 '11 at 16:49.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.