0
char words[wordCount][MAX_WORD_LENGTH];

I know i need to use malloc to be able to have a user defined length of an array but how would i do this with a 2d array?

char words[MAX_WORD_LENGTH];
words = (words *) malloc (wordCount)

Would something like that work? Just started using c so im still getting used to it.

6
  • Are you on Windows with MSVC (a C89 compiler), or on Unix with a C99 or C2011 compiler? If the latter, then you don't have to do dynamic memory allocation; you can use a VLA (variable length array). Commented Jan 24, 2013 at 5:01
  • @DeepankarBajpeyi: That (char *words[wordCount];) is a 1D array of pointers. Commented Jan 24, 2013 at 5:02
  • @Jonathan Leffler, "you can use a VLA (variable length array)" - its size is too small & you can't realloc it. Better to do "hand-made" allocation. Commented Jan 24, 2013 at 5:02
  • @Eddy_Em: why would you make your VLA too small? You make it exactly the size you need! Commented Jan 24, 2013 at 5:03
  • @Jonathan Leffler, this: char str[1000][10000];strcpy(str[9999], "string"); doesn't work. Commented Jan 24, 2013 at 5:08

5 Answers 5

0

You have to do this first:

char *words = calloc(wordCount * MAX_WORD_LENGTH, sizeof(char));

and access words[i][j] as words[i*MAX_WORD_LENGTH + j]

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

6 Comments

char *words = (char *)calloc(wordCount * MAX_WORD_LENGTH, sizeof(char)); This makes sense, i looked up calloc and and it looks like the right function for the job. I will test it out.
cool @mashedtatoes. I don't think you have to cast the calloc return
wont compile since calloc returns a void*
@mashedtatoes what compiler is this? C or C++? in C that's perfectly legal statement. You can see the answer by Jonathan Leffler too, he does not cast the malloc. And there is a good reason for it
Well that fixed some of my other problems. My compile hotkey was set to use g++ and not gcc
|
0

This link may help

Pointers can be easily used to create a 2D array in C using malloc. The idea is to first create a one dimensional array of pointers, and then, for each array entry, create another one dimensional array.

Another answer is present in the stack overflow it self malloc() for a 2D array (using pointer-to-pointer)

2 Comments

why use a malloc when we have calloc?
The drawback to using calloc() is that it takes time to clear memory, and in most cases, you don't need it clear since you'll just be writing over it anyway. But if you ever find yourself malloc()ing a block and then setting the memory to zero right after, you can use calloc() to do that in one call Source :- Beej's guide to C
0

If you want to index the array using two subscripts words[i][j], then you need to do two allocations (well, you could make it one if you're really confident in your handling of pointers, but you'd do better with two if you're asking this question).

You need to allocate enough space for the pointers, and enough space for the data:

char **words = malloc(wordCount * sizeof(*words));
char  *data  = malloc(wordCount * MAX_WORD_LENGTH);

if (words == 0 || data == 0)
{
    free(data);
    free(words);
    ...report error?...
}

for (int i = 0; i < wordCount; i++)
    words[i] = data + (i * MAX_WORD_LENGTH);

...now you can use words[i][j] to access the jth character of the ith word...

Comments

0

If you want to have user defined size for each word pointer in the words array :

char * words[wordCount];
for (i =0; i< wordCount; i++)
words[i] = malloc(sizeof(char)*User_defined_Size);

3 Comments

Why not ? It returns void* and words[i] would be char * ?
ah well, now if you modify your answer, I shall remove the downvote
-1
char **words = NULL;
words = (char **)malloc(sizeof(char*) * user_defined_size_A);
for(i = 0; i < user_defined_size_A; i++) {
   words[i] = (char*) malloc(sizeof(char) * user_defined_size_B);
}

you can try this...

1 Comment

Thou shalt not cast return value from malloc()

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.