1

I'm currently learning C and I'm working on a program that reverses the order of words in a sentence using 1D arrays.

The way it is supposed to function is as follows:

Input: this is a program

Output: program a is this

But when I try to run it I get an error saying "Segmentation fault" which I assume means the program is trying to access memory that it shouldn't.

GDB told me the problem was at the printf in my loop

Here's my code:

#include <stdio.h>
#define N 99

//initialize i at 1 to leave space in beginning
int i=1;

//j is for counting charachters of individual words
int j=0;

//initailize char array with spaces
char array[N]={' '};


int main(void)
{


printf("Enter a sentence ended by a [. or ! or ?]: ");


//enter sentence charachter by charachter into an array. end at terminator.
for (i=1 ; ; i++ )
{
    array[i]=getchar();
    if (array[i]=='.' || array[i]=='!' || array[i]=='?')
    {
        break;
    }

}


//begin loop.
for ( ; array[i] != array[0] ; )
{
//note current position of i into j. j is end marker.
    j=i;

//search backward from j for the beginning of last word (after a space).
    for (i=j ; array[i]!= ' ' ; i--)
    {

    }
    i++; //move to beginning of word

//print word to end counting places moved. stop at j.
    for ( ; i!=j ; i++)
    {
        printf("%c", array[i]);
        j++; //increment j to determine number of charachters.
    }


//move back the same amount of places moved.
    for ( ; j!=0 ; j--)
    {
        i--;
    }
    //i--; //move back once more

//update j to new position.

//start loop again.
}


return 0;
}
TomaszS
  • 57
  • 7
  • `for (; i != j; i++) { printf(...); j++; }` -- this is an infinite loop. run your code in your head (i.e. step through each line and keep track of the values of the variables) in order to find the bugs. – Adrian Oct 16 '15 at 02:56
  • Wow this stumped me for so long. I feel like an idiot now. That is so obvious. Thanks for the help! :) – TomaszS Oct 16 '15 at 02:59
  • 1
    Judging by the comment before it, you'd be surprised what `char array[N]={' '};` actually does. – EOF Oct 16 '15 at 03:18
  • It initializes an array with spaces? Is that not right? – TomaszS Oct 16 '15 at 03:21
  • 1
    @TomaszS: No. It initializes *the first element* of the array with a space, and all the rest to zero. C11 draft standard `6.7.9 Initialization, Section 19 The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed initializer for the same subobject; 151) all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.` and `Section 10 [...]if it has arithmetic type, it is initialized to (positive or unsigned) zero[...]`. – EOF Oct 16 '15 at 03:25
  • Oh I see. So if I want to make all the cells in the array to spaces I would have to do {" ", " ", " ", ect}. Does C not have a way to initialize all values to something other than 0? – TomaszS Oct 16 '15 at 03:31
  • 1
    @TomaszS: `memset(array, ' ', N);`. – EOF Oct 16 '15 at 03:32
  • Possible duplicate of [Definitive List of Common Reasons for Segmentation Faults](http://stackoverflow.com/questions/33047452/definitive-list-of-common-reasons-for-segmentation-faults) – CodeMouse92 Oct 16 '15 at 14:48

1 Answers1

0
for ( ; i!=j ; i++)
{
    printf("%c", array[i]);
    j++; //increment j to determine number of charachters.
}

In this loop is infinite loop. Because you are increasing i and j both. So, in here the j and i does not become same in any time. So, only you are getting a segmentation fault error.