-3
#include <stdio.h>
#include <string.h>

int main()
{
    char *input[1000];
    char *word;
int i = 0;
while (scanf("%s", word) != EOF)
{
    input[i] = word;
    i++;
}
printf("%s ", input[0]);
printf("%s ", input[1]);
printf("%s ", input[2]);
}

for example, if the input is "My name is", the output would be "is is is" as opposed to "My name is". Can anyone point out where in my code is wrong.

3 Answers3

3

There are some issues here, let me try to address them

with the following you are declaring an array of char pointers but those pointers are not pointing anywhere or rather they are pointing everwhere ;-).

char *input[1000];

now you declare a pointer, which left uninitialized, points anywhere

char *word;

now you read from the keyboard using the uninitialized pointer

while (scanf("%s", word) != EOF)

What you need to do allocate memory for what you read

First have a temporary buffer to read from the keyboard

char word[100];

Now while reading from the keyboard copy the contents of word and allocate memory for the contents.

input[i++] = strdup(word);

Do not forget to free the memory that input[] are pointing too, it is also good if you initialize the input[] pointers so you can distringuish between used pointers and uninitialized pointers.

char* input[1000] = { NULL };
AndersK
  • 35,813
  • 6
  • 60
  • 86
  • And to prevent buffer overflow, limit the reading of the word by changing %s to %100s, see http://stackoverflow.com/questions/1621394/how-to-prevent-scanf-causing-a-buffer-overflow-in-c – Roalt Feb 05 '16 at 07:12
  • @Roalt yep, good point. personally i would have used fgets instead since it is anyway just strings. – AndersK Feb 05 '16 at 07:29
0

You have not allocated memory for word. The code invokes undefined behavior. Allocate some memory like this:

int stringSize = 10;
char *word = malloc(sizeof(char)*stringSize );

or

char  word [10];
Sadique
  • 22,572
  • 7
  • 65
  • 91
  • Ok, i see what you mean. How can I fix it to get the desired output? – Oscar Shum Feb 05 '16 at 06:54
  • All the inputs points at the memory address of word currently, right? – Oscar Shum Feb 05 '16 at 06:56
  • Is there another way to do it without malloc. This hasn't been covered yet. – Oscar Shum Feb 05 '16 at 06:57
  • You could use `char word[20];` . But then the line `input[i] = word;` will fail. So you do not have much choice really, you have to either use malloc, or use `char input[1000][20];` instead too. – M.M Feb 05 '16 at 06:58
0

word is a pointer to an array of chars, which will point to your first input "My" on the first iteration. This means that:

word = "My";
input[0] = word;
input[0] = "My"; //as word and input[0] now point to the same memory location

You're then modifying the value stored at the location in memory pointed to by word:

input[0] = word;
word = "name";
input[1] = word;

word, as a pointer has not changed, only the value it points to has changed. On your second iteration:

input[0] = word = "name"; //as the memory location of word doesn't change
input[1] = word = "name"; //any pointer set to word will point to the same char array

Your third iteration does the exact same thing, causing your output to be "is is is".

First, you should define word with its own size/memory:

char word[64] = NULL; //arbitrary size that's bigger than your largest length word

For this example you can use:

char input[5][64] = { NULL };

But I would suggest using dynamically allocated memory for input later on.

And it's up to you how you copy the contents of word to your input array, but possibilities include using strcpy:

int i = 0;
while (scanf("%s", word) != EOF){
    strcpy(input[i], (const char*)word);
    i++;
}
Jarred
  • 67
  • 6