# Scanf confusion, 2 dimensional array modification

This is a discussion on Scanf confusion, 2 dimensional array modification within the C Programming forums, part of the General Programming Boards category; So a dynamic two-dimensional array using malloc would be this: Code: int **two_d_array; two_d_array = malloc(rows * sizeof(int *)); for ...

1. So a dynamic two-dimensional array using malloc would be this:
Code:
```int **two_d_array;
two_d_array = malloc(rows * sizeof(int *));
for (int i = 0; i < rows; i++) {
two_d_array[i] = malloc(columns * sizeof(int));
}
return two_d_array;```
I think I have all the keywords, so that should show up on a search. Later you will have to do this:
Code:
```for (int i = 0; i < rows; i++) {
free(two_d_array[i]);
}
free(two_d_array);```
So make sure you keep hold of the pointer.

2. Ok, what you have said seems to be working. but I don't know why x** and what the function returns are incompatible? I've declared the function to return an int**.

3. int **x and the function are compatible. It's matrix[3][3] that isn't.

4. Ok. To make this absolutely clear.
How can I take what the function returns, and assign it to a two dimensional array??

5. No. You can assign it to a double-star. Arrays (of any kind) are non-assignable at all times. That is,
Code:
```int a[5];
a = //there's nothing you can put here that would make it right```

6. So why we are talking about function that return pointer to arrays since we cannot assign them to arrays.??
And if you have a way to do that in mind, please suggest it. I hope you do realize the time difference between us.
Thanks,

7. Originally Posted by Leojeen
So why we are talking about function that return pointer to arrays since we cannot assign them to arrays.??
And if you have a way to do that in mind, please suggest it. I hope you do realize the time difference between us.
Thanks,
So this whole boondoggle requires you to completely forget that arrays exist. Everything and I mean everything must be int**. Once you write int matrix[4][4], you're sunk. If you consistently use double-stars, you can assign double-stars to your heart's content.

8. Ok, so it worked after declaring matrix itself as an int**. I got your point finally. Thank you for u time!!

9. Incidentally, there is another way to do this: create a single huge (dynamically allocated) array, and then use it as a two dimensional array by computing offsets yourself. The good part is that you only malloc() and free() once and get contiguously allocated space; the bad part is that you have to compute the offsets yourself and the chance of there being insufficient space left to allocate is higher since the space must be allocated contiguously.

Page 2 of 2 First 12