By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,952 Members | 1,119 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,952 IT Pros & Developers. It's quick & easy.

malloc a 2D array

P: n/a
Hey all,

Im use to using malloc() with a one-dimensional array. But I have
found the need to use a 2D array, and would like to confirm whether I
am allocating memory correctly.

As I can gather, unlike a 1D array, you cannot allocate memory to a 2D
array with only 1 line of code.

Just say i wish to have an array of strings, I wish to declare an
array like arrayString[10][20], where I will be able to hold 10
strings, each string of a maximum of 20 characters (as an example).

Can I do this?

************************************
arrayString = malloc(10);

int i;
for (i = 0; i< 10; i++)
{
arrayString[i] = malloc(20);
}
************************************

Would this be ideal, or is there a better solution to this problem?

Thank you all for your help.. Much appreciated!
mike79
Nov 13 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
mike79 wrote:
Im use to using malloc() with a one-dimensional array. But I have
found the need to use a 2D array, and would like to confirm whether I
am allocating memory correctly. As I can gather, unlike a 1D array, you cannot allocate memory to a 2D
array with only 1 line of code.
Depends how you do it.
Just say i wish to have an array of strings, I wish to declare an
array like arrayString[10][20], where I will be able to hold 10
strings, each string of a maximum of 20 characters (as an example).
For consistency with your code I will assume you mean 19 characters
plus the terminating \0.
Can I do this? ************************************
arrayString = malloc(10);

int i;
for (i = 0; i< 10; i++)
{
arrayString[i] = malloc(20);
}
************************************
arrayString is an array of pointers to char. Hence:
char **arrayString;
arrayString = malloc(10 * sizeof (char *));

Usual caveats about checking the return value from malloc apply.

This way requires a loop to allocate, but allows the usual
arrayString[i][j] usage.
Would this be ideal, or is there a better solution to this problem?


If you want to avoid the loop, you can just make arrayString a
pointer to char, malloc(10*20), and do pointer arithmetic. I'd say
the loop is easier all around.

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #2

P: n/a
mike79 <mi****@iprimus.com.au> wrote:
Hey all,

Im use to using malloc() with a one-dimensional array. But I have
found the need to use a 2D array, and would like to confirm whether I
am allocating memory correctly.

As I can gather, unlike a 1D array, you cannot allocate memory to a 2D
array with only 1 line of code.

Just say i wish to have an array of strings, I wish to declare an
array like arrayString[10][20], where I will be able to hold 10
strings, each string of a maximum of 20 characters (as an example).

Can I do this?

************************************
arrayString = malloc(10);
Nope. arrayString should be declared as:

char **arrayString;

and so your malloc is allocating space for 10 (char *)s, not 10 chars:

arrayString = malloc(10 * sizeof *arrayString);
int i;
for (i = 0; i< 10; i++)
{
arrayString[i] = malloc(20);
}
************************************

Would this be ideal, or is there a better solution to this problem?


If the size of strings is known at compile-time, and you aren't going to
be swapping strings around inside the array, this is probably better:

char (*arrayString)[20];

arrayString = malloc(10 * sizeof *arrayString);

.... that's it.

There are some more methods in the comp.lang.c FAQ.

- Kevin.

Nov 13 '05 #3

P: n/a
mike79 wrote:

Hey all,

Im use to using malloc() with a one-dimensional array. But I have
found the need to use a 2D array, and would like to confirm whether I
am allocating memory correctly.

As I can gather, unlike a 1D array, you cannot allocate memory to a 2D
array with only 1 line of code.

Just say i wish to have an array of strings, I wish to declare an
array like arrayString[10][20], where I will be able to hold 10
strings, each string of a maximum of 20 characters (as an example).


/*
** Make a pointer to the first element of the two dimensional array:
** char (*arrayString_ptr)[20];
** arrayString_ptr = malloc(10 * sizeof *arrayString_ptr);
*/

/* BEGIN new.c */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
char *number[] = {"one","two","three","four","five","six",
"seven","eight","nine","ten"};
char (*arrayString_ptr)[20];
int word;

arrayString_ptr = malloc(10 * sizeof *arrayString_ptr);
if (!arrayString_ptr) {
fputs("I'm tired.\n", stderr);
exit(EXIT_FAILURE);
}
for (word = 0; word != 10; ++word) {
strcpy(arrayString_ptr[word], number[word]);
}
for (word = 0; word != 10; ++word) {
puts(arrayString_ptr[word]);
}
free(arrayString_ptr);
return 0;
}

/* END new.c */
Nov 13 '05 #4

P: n/a
Kevin Easton wrote:
arrayString = malloc(10 * sizeof *arrayString);
Whoops, my bad. I knew better, too. Do it this way, not the way I
wrote it.
If the size of strings is known at compile-time, and you aren't going to
be swapping strings around inside the array, this is probably better: char (*arrayString)[20]; arrayString = malloc(10 * sizeof *arrayString);


Perhaps it's just unfamiliarity, but I find this hard to understand.
I think the malloc-twice way is much more intuitive. Opinions?

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #5

P: n/a
pete wrote:
char *number[] = {"one","two","three","four","five","six",
"seven","eight","nine","ten"};


A C programmer who starts counting at one. How unusual... :)

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #6

P: n/a
Tom Zych wrote:

Kevin Easton wrote:

char (*arrayString)[20];

arrayString = malloc(10 * sizeof *arrayString);


Perhaps it's just unfamiliarity, but I find this hard to understand.
I think the malloc-twice way is much more intuitive. Opinions?


it's just unfamiliarity
Nov 13 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.