0

I'm having big problems while creating a simple program in C.

I have the following function:

void createStrings (char *dictionary[], int *n) {
int i;
char word[20];

printf ("Insert how many words to use: ");
scanf ("%d", &(*n));

// Initialization
for (i = 0; i < *n; i++) {
    dictionary[i] = '\0';
}

// Populating the array with words
for (i = 0; i < *n; i++) {
    printf ("Insert the word in position %d: ", i);
    scanf("%s", word);
    dictionary[i] = word;
}
}

In the main I read the array of words just populated by using

printf ("Following words have been inserted:\n");
for (i = 0; i < n; i++) {
    printf ("dictionary[%d] = %s\n", i, dictionary[i]);
}

I'm pretty sure this last for cycle has been implemented well.

When I run the program and try to insert, for example, three different words like "one" "two" and "zero" I get the following output:

Insert how many words to use: 3

Insert word number 0: One

Insert word number 1: Two

Insert word number 2: Zero

Following words have been inserted:

dictionary[0] = Zero

dictionary[1] = Zero

dictionary[2] = Zero

It's like only the last word I inserted gets saved and the cycle goes all the way back to override all the other elements of the array.

Any help would be greatly appreciated.

3 Answers 3

2

Don't use = to assign a string. Allocate memory for it first, then copy.

Sign up to request clarification or add additional context in comments.

Comments

1

char word[20]; is a single storage location: you're pointing each successive dictionary[i] to that same location, but overwriting it with a new value. So, you just get an array of pointers to a chunk of memory (which you shouldn't really access after createStrings returns anyway, since it's local to that function).

Change

dictionary[i] = word;

to

dictionary[i] = strdup(word);

for the smallest possible change.

I can think of more improvements, but they probably belong on CodeReview, and this should be sufficient to make it work.

3 Comments

Thanks a lot, this solved my problem! Thanks also to everyone else!
If adding strdups is all you do, you introduce memory leaks.
True, but there are many possible issues with this code; hence I suggested taking it to CodeReview instead of addressing them all here.
0

You have many problems in your code:

  • You ask the user how many words they want to enter and then you put this many words into an array of unknown size. The user might have entered a number of words that is larger than the array.
  • You allow the user to enter any number of characters per word but your string can accept only 20.
  • You store the entered words in a local variable inside a function, and code outside this function attempts to access it after the function has returned and the stack space for this variable is no longer allocated.

Any of these problems can cause your code to crash or allow a user to overwrite areas of memory they're not supposed to. You need to fix these problems.

As the other answerers have said, you need to use strdup or something to independantly store each string on the heap.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.