user11246874
user11246874

Reputation:

program outputs exit phrase instead of words stored in C

So I was working on an assignment for school, and had written up a variation of this code:

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX 100

// This program takes an input of strings and prints them out with a new line separating each one.

int main() {
    char *WordArray[MAX];       //initializing variables
    int i = 0;
    int count = 0;

    printf("enter up to 100 words, that are 20 characters maximum \n");     

    for (i = 0; i <100; i++){                   //runs while there's less than 100 inputs
        char Array[1];
        scanf("%s",Array);                      //stores string in the array
        if (strcmp(Array, "STOP") == 0) {       //compares the string with stop, and if it is, it breaks out of the loop
                 break;
        }
        WordArray[i]=Array;                     //stores the string in the pointer array

    }
    printf("The output is\n");
    for (count = 0; count<i; count++){          //counts up to the amount of words stored
        printf("%s\n",WordArray[count]);        //outputs each pointer string
    }
}

and I noticed that the output was printing "STOP" instead of the values stored. Anyone have any answers to why and/or how to fix it? I know one of the methods is to switch to a 2D array instead of using pointers, but I'm still baffled as to why a program like this wouldn't work.

Upvotes: 0

Views: 34

Answers (1)

Weather Vane
Weather Vane

Reputation: 34585

Your char Array[1]; isn't large enough to store any but an empty string. Also, when it works, every pointer will point to the same string, which will be the last entry you made. This makes some corrections where commented.

#include <stdio.h>
#include <stdlib.h>                         // instead of ctype.h
#include <string.h>

#define MAX 100

// This program takes an input of strings and prints them out with a new line separating each one.

int main() {
    char *WordArray[MAX];    
    int i = 0;
    int count = 0;

    printf("enter up to 100 words, that are 20 characters maximum \n");     

    for (i = 0; i <100; i++){
        char Array[21];                     // increase size of array
        scanf("%20s",Array);                // limit entry length
        if (strcmp(Array, "STOP") == 0) { 
                 break;
        }
        WordArray[i] = strdup(Array);       // allocate memory for and copy string

    }
    printf("The output is\n");
    for (count = 0; count<i; count++){    
        printf("%s\n",WordArray[count]);  
    }

    // free each string's memory
    for (count = 0; count<i; count++){    
        free(WordArray[count]);
    }
}

Program output:

enter up to 100 words, that are 20 characters maximum
one two three STOP
The output is
one
two
three

Edit: note that your code contains another undefined behaviour besides the too-short string char Array[1] which is that you dereference the pointer you stored in char *WordArray[MAX];. The scope of Array is inside the for loop, and theoretically ceases to exist after the loop completes, so the pointer you store is invalid. Here, the word entered is duplicated with strdup so that doesn't apply.

Upvotes: 1

Related Questions