0

My program takes in the Constitution, reverses the line order, and prints the Constitution in reverse order back in the command line. When I put the algorithm to reverse the lines in a separate function, lineReversal, the only thing that prints is ▒▒▒.

int main(){

char * buffer;

char * strings[1000]; 


int arrayCount =0;
int size = 10;

while(!feof(stdin))
{
    buffer= (char*) malloc(size);
    getAline(&buffer, &size); //gets each line from the Constitution

    strings[arrayCount++] = buffer;
}

lineReversal(&strings);

return 0;
}

char lineReversal(char ** stringPtr)
{
char * strings = *stringPtr;

for(int i = 873; i >=0 ; i--) {
    if(strings[i] == '\0'){
        break;
    }
    printf("%s", strings + i);
}

*stringPtr = strings;
return 0;
} 

If I put that algorithm into main() and run my program, this is my output:

H▒▒▒▒▒▒▒▒▒▒▒▒Dhave intervened.Representatives, shall take effect, until an election of Representatives shall

The expected output is:

have intervened.

This is what my program looks like with the algorithm inside main.

int main(int argc, char ** argv){

char * buffer;

char * strings[1000]; 


int arrayCount =0;
int size = 10;

while(!feof(stdin))
{
    buffer= (char*) malloc(size);
    getAline(&buffer, &size);

    strings[arrayCount++] = buffer;
}

for(int i = 873; i >=0 ; i--)
    {
    if(strings[i] == '\0'){
        break;
    }
    printf("%s", strings[i]);
}

return 0;
}
Emma Barrett
  • 83
  • 1
  • 10
  • Does it print **junk**? :) – gsamaras Oct 10 '16 at 19:28
  • 2
    `i = 873`?? Magic numbers in code are very bad. What is 873 supposed to be?? – kaylum Oct 10 '16 at 19:32
  • 1
    For one thing, please see [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). For another there are probably very few text lines in your document which have only `9` characters - `int size = 10; ... buffer= (char*) malloc(size);` – Weather Vane Oct 10 '16 at 19:33
  • `getAline` looks suspicious, why do you pass the address of `buffer`? buffer itself is already pointing to the buffer that you should pass. you also would be better off setting the last entry to point to NULL, that way you do not need to keep track of some magic number 873 i.e. `for (int i =0; strings[i] != NULL; ++i) {...}` – AndersK Oct 10 '16 at 19:33
  • @kaylum 873 is the number of lines in the Constitution. I realize this is bad code, it was just a quick way for me to loop backwards through the array. – Emma Barrett Oct 10 '16 at 19:40
  • So you are asking why code that you realise is bad, does not work? – Weather Vane Oct 10 '16 at 19:48
  • Curious: What reference (person, book, URL, etc. ) suggested to use `while(!feof(stdin))`? – chux - Reinstate Monica Oct 10 '16 at 20:01
  • @Anders K. `getAline()` looks like a modified `getline()`. Passing the address of the buffer pointer and address of the size is required with that function. – chux - Reinstate Monica Oct 10 '16 at 20:04
  • @chux My C professor recommended we use it – Emma Barrett Oct 10 '16 at 21:15
  • @Emma Barrett Thanks. If the prof recommended exactly as you used it, watch out for subsequent "good ideas" from the prof. – chux - Reinstate Monica Oct 10 '16 at 22:13
  • I suspect the issue is with `getAline(&buffer, &size);`, check its return value. Suggest change to `for(int i = arrayCount; i >0 ; ) { printf("%s", strings[--i]); }` – chux - Reinstate Monica Oct 10 '16 at 22:17

0 Answers0