M
mike79
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 = malloc(20);
}
************************************
Would this be ideal, or is there a better solution to this problem?
Thank you all for your help.. Much appreciated!
mike79
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 = malloc(20);
}
************************************
Would this be ideal, or is there a better solution to this problem?
Thank you all for your help.. Much appreciated!
mike79