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.

Doug
  • 149
  • 2
  • 11
  • 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). – Jonathan Leffler Jan 24 '13 at 05:01
  • @DeepankarBajpeyi: That (`char *words[wordCount];`) is a 1D array of pointers. – Jonathan Leffler Jan 24 '13 at 05: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. – Eddy_Em Jan 24 '13 at 05:02
  • @Eddy_Em: why would you make your VLA too small? You make it exactly the size you need! – Jonathan Leffler Jan 24 '13 at 05:03
  • @Jonathan Leffler, this: `char str[1000][10000];strcpy(str[9999], "string");` doesn't work. – Eddy_Em Jan 24 '13 at 05:08
  • @Eddy_Em: If you need a 10 MiB array, yes, you may have problems (on some machines). You have to validate user input. But for many reasonable scenarios, a VLA is perfectly usable. You need to do basic validation on the sizes with dynamic allocation. There's no indication in the question that the 'words' might be 10 KB long, or that there might need to be 1000 of them. Also, with words of 10 KB, there's a very good chance that most of that allocated space would be wasted. VLA is merely an option to be considered. – Jonathan Leffler Jan 24 '13 at 05:10

5 Answers5

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)

Community
  • 1
  • 1
Dinkar Thakur
  • 3,025
  • 5
  • 23
  • 35
  • why use a malloc when we have calloc? – Aniket Inge Jan 24 '13 at 05:05
  • 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` – Dinkar Thakur Jan 24 '13 at 09:42
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]

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • `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. – Doug Jan 24 '13 at 05:08
  • cool @mashedtatoes. I don't think you have to cast the calloc return – Aniket Inge Jan 24 '13 at 05:11
  • wont compile since calloc returns a void* – Doug Jan 24 '13 at 05:17
  • @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 – Aniket Inge Jan 24 '13 at 05:18
  • Well that fixed some of my other problems. My compile hotkey was set to use g++ and not gcc – Doug Jan 24 '13 at 05:34
  • @mashedtatoes now, if you would be so kind and select the answer... :-) – Aniket Inge Jan 24 '13 at 05:52
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);
Deepankar Bajpeyi
  • 5,661
  • 11
  • 44
  • 64
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...
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
-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...