# Thread: Help with 2-D Arrays and Malloc

1. ## Help with 2-D Arrays and Malloc

Hi guys,

I seriously do not understand how to use malloc to create a two-dimensional array. I understand the concept of malloc and how you can use a pointer to basically create a 1-D array using it. The problem is, I do not understand this pointers to pointers stuff, I don't understand the syntax or how to get it to function properly. I've been searching for help online but nothing has really addressed my specific issue. Can someone be kind enough to either tell me a good online source of help for this or give me a quick run-down (if they are good enough to simplify this problem quickly)? Thanks

2. Okay short updated, I found a site that was very helpful and I get the jist of it.

Now for the next problem: I'm trying to create the dynamic array using malloc in the main function, and then call another function to actually fill the array and print the values. I'm getting an error message when doing it and it won't compile. Can anyone help? Thanks

Code:
```#include <stdio.h>
#include <stdlib.h>

int main()

{ int **createMatrix(int **array,int rows, int columns);
int **array;
int rows=2;
int columns=2;
int i;
array=(int**)malloc(rows*sizeof(int*));

for (i=0;i<rows;i++)
array[i]=(int*)malloc(columns*sizeof(int));
createMatrix(array,rows,columns);

}
int **createMatrix(int **array,int rows, int columns)
{   int i,j;
for (i=0;i<rows;i++)
printf("\n");
{
for(j=0;j<columns;j++)
array[i][j]=2;
printf("%d ",array[i][j]);
}
}```

3. Code:
`int **createMatrix(int **array,int rows, int columns);`
This line should not be in main, it should be above it. You do not define functions inside other functions.

main() should return an integer and be declare as

Code:
`int main(void)`

4. Okay, thanks for that.

The bad news is, the program still won't run. I think it might have to do with how I sent the parameter to the function. Is that the proper way of assigning the argument to the double pointer parameter?

5. What error message do you get?

Note that while it doesn't matter for this particular program, you need to free the memory you malloc. In anything more complex not freeing would result in memory leaks.

6. It's a runtime error, the program compiles but then before printing anything it gives a pop-up saying 'whatever.C has stopped working', similar to the message you would get if you forgot the & in a scanf statement.

7. Code:
```int **createMatrix(int **array,int rows, int columns)
{   int i,j;
for (i=0;i<rows;i++)
printf("\n");
{
for(j=0;j<columns;j++)
array[i][j]=2;
printf("%d ",array[i][j]);
}
}```
Your problem comes from not using braces where you should:

in the first loop over i, since you have no braces, you are just printing a newline i times. Then you are looping over the array column index, but i is already passed rows, so array[i] is no longer valid.

So, put the brace that is under the printf("\n"); just above it and try again.

Let this be a lesson to you: Always (no matter what, even if you don't need them) put braces in around all blocks of code that could conceivably need them. It helps break up the code visually, and you will never have something silly like this occur.

In the future: when you get a segmentation fault when you are using arrays that are dynamically allocated, chances are you ran off the end of one of the dimensions.

Also: since your createMatrix is not returning anything, why does your declaration of it say it will be returning int **?

8. Thanks, the problem is solved!

The double pointer return was a leftover from something else I was doing, I never bothered to change it.

Thanks again.