- #1
ashwinnarayan
- 18
- 0
Homework Statement
Ok, I'm learning C programming and I'm trying to create a dynamic 2D array. This is the code that seems to work in creating a dyamic array. I found it in a book.
Code:
//rows and cols are values entered by the user when the program runs.
int **(matA) = malloc(rows*sizeof(int *));
matA[0] = malloc(rows*cols*sizeof(int));
for(i=1;i<rows;i++) matA[i] = matA[0] + (i*cols);
My question is: What is the function of the statement sizeof(int *)?
I ran the program with a printf statement to print the value of sizeof(int *) and it came up as 8. I don't understand why this first malloc statement even exists. If I delete the first malloc statement then the code doesn't run.
Last edited: