1

When I compile this code as it is, I get this error:

variable-sized object 'word_list' may not be initialized
   char *word_list[i] = malloc(sizeof(char) * STRING_LENGTH );

I know on line 13 my variable word_list is just a pointer to characters and that it is causing the problem. I have to use an array of pointers instead of it but I am not sure how to do this properly. The word_list variable is supposed to be an array that is to have strings copied into it using strcpy.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define STRING_LENGTH 20
#define MAX 30

int read_string(char string[], int n);
int compare(const void*a, const void*b);

int main(){
int i;

char* word_list;
char word[STRING_LENGTH + 1];

for (i = 0; ; i++){
    printf("\nEnter a word.\n");
    read_string(word, STRING_LENGTH);

    if (word[0] == '\0')
        break;
    char *word_list[i] = malloc(sizeof(char) * STRING_LENGTH );
    free(word_list);
    strcpy(word_list[i], word);
    }

    int length = sizeof(word_list)/sizeof(char*);

    int j;
    for (j = 0; word_list[j] != '\0'; j++)
    printf("%s\n", word_list[j]);

    qsort(word,length, sizeof(char*), compare);
    for (j = 0; word_list[j] != '\0'; j++)
    printf("%s\n", word_list[j]);

    return 0;
    }




    int compare(const void*element1, const void *element2){
    const char *string1 = *(const char**)element1;
    const char *string2 = *(const char**)element2;

    return strcmp(string1,string2);
    }

    int read_string(char string[], int n){
    int ch, i = 0;

    while ((ch = getchar()) != '\n')

    if (i < n)
        string[i++] = ch;
    string[i] = '\0';

    return i;
    }
1
  • 1
    why are you calling free(word_list) immediately after you (try to) allocate it? Commented Jul 8, 2015 at 22:54

2 Answers 2

2

So if I am understanding correctly, you want an array of strings? (note this is just an array of pointers and you still need to malloc the space for each individual string)

So

char *word_list[NUM_STRINGS];
word_list[i] = malloc(strlen(word) + 1);
strcpy(word_list[i], word);

But it looks like all elements in your array will be the same length? In which case you can just use something like this, without the need for malloc or free:

char word_list[NUM_STRINGS][STRING_LENGTH+1];
strcpy(word_list[i], word);

If you're looking to have it loop indefinitely while their is user input then you won't be able to accomplish this easily with an array (only if you care about them going over the NUM_STRINGS limit). A linked list would work better in my opinion.

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

Comments

1

You declared word_list as a character pointer, not an array of same. If your malloc is intended to allocate space for one of the elements of an array, you need to create the array first. (The line with the error looks like you are declaring a variable.)

If you know how big this array needs to be, you should be all set. But if you don't, you might want to look into how realloc works.

3 Comments

I am just suppose to use malloc
malloc is now causing an error. invalid conversion from void* to char*
@Flower that error indicates you are using a C++ compiler instead of a C compiler (the two languages are different)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.