Expand a C array inside a function - c

This works in a main, but breaks when put into a function. I'm not sure how to reassign the pointer after passing into a function.
void expandArray(int** arr[], int* size) {
int *temp;
*temp = *arr;
*arr = (int*) malloc(*size * 2 * sizeof(int));
for (int i = 0; i < *size; i++) {
printf("assigning from temp: %d ", temp[i]);
arr[i] = temp[i];
printf("to arr: %d \n", arr[i]);
}
*size = *size * 2;
free(temp);
}
main(){
int *arr;
arr = (int*) malloc(maxSize * sizeof(int));
if ....
expandArray(arr, &arrSize);
// use bigger arr for other stuff
}

In main, arr is declared as int *arr. When main calls expandArray, it should pass a pointer to arr, which is written &arr and has type int **a.
However, you declared the parameter to expandArray as int **arr[], adding additional brackets. Those are unnecessary and change the type, and your compiler should have warned you about that. Pay attention to compiler warnings. Be sure you understand them, and resolve them before proceeding.
In expandArray, you use both arr[i] and temp[i] to access the array. However, arr[i] is not a correct way to access array elements. When the declaration of the arr parameter is corrected, it will be int **arr, and it will not be proper to refer to an element of the array as arr[i]. It will be (*arr)[i].
Commonly, to make this a little less confusion, authors will use a temporary variable to hold the pointer, so they do not need the extra asterisk:
int *NewArray = malloc(...); // Get new space.
*arr = NewArray; // Send new address to caller.
...
NewArray[i] = temp[i]; // Use temporary variable for access.
Some other points:
When calling malloc, use sizeof *p, where p is the pointer being assigned to, rather than sizeof(int). This is better because, if you later want to change the type for p, it only has to be changed in its declaration, not also in the sizeof. Then there is less likely to be a mistake where it is changed in one place and not another.
Do not cast the result of malloc. This is unnecessary in C, although it is required in C++.
main should be declared as int main(void) or int main(int argc, char *argv[]), not as main(). (C implementations may also provide for other forms.)
Use size_t for sizes of arrays, not int, and either size_t or ptrdiff_t for indices of arrays.
Overall, the code could be:
#include <stdio.h>
#include <stdlib.h>
void expandArray(int **arr, int *size)
{
// Record old pointer and size in temporary variables for convenience.
int *OldArray = arr;
size_t OldSize = *size;
// Prepare new size and pointer.
size_t NewSize = 2 * OldSize;
int *NewArray = malloc(NewSize * sizeof *NewArray);
// Handle allocation failure.
if (!NewArray)
{
fprintf(stderr, "Error, unable to allocate memory.\n");
exit(EXIT_FAILURE);
}
// Copy data from old array to new array.
for (size_t i = 0; i < OldSize; ++i)
NewArray[i] = OldArray[i];
// Send new size and pointer to caller.
*size = NewSize;
*arr = NewArray;
// Release old memory.
free(OldArray);
}
int main(void)
{
int *arr;
arr = malloc(InitialSize * sizeof *arr);
if (...)
expandArray(&arr, &arrSize);
// use bigger arr for other stuff
}

I think the arr sould be int ** type.
void expandArray(int** arr, int* size) {
int *temp;
temp = *arr;
*arr = (int*) malloc(*size * 2 * sizeof(int));
for (int i = 0; i < *size; i++) {
printf("assigning from temp: %d ", temp[i]);
(*arr)[i] = temp[i];
printf("to arr: %d \n", (*arr)[i]);
}
*size = *size * 2;
free(temp);
}
If you just want to expand the array size, you can use realloc.
And the extended area should be initialized using memset.
void expandArray(int** arr, int* size) {
*arr = (int*) realloc(*arr, *size * 2 * sizeof(int));
memset(*arr+*size, 0, *size * sizeof(int));
*size = *size * 2;
}

Related

different ways to declare a matrix c

I don't really understand why method 1 works but not method 2. I don't really see why it works for characters and not an int.
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
/// WORK (METHODE 1)
char **string_array = malloc(sizeof(char **) * 10);
string_array[0] = "Hi there";
printf("%s\n", string_array[0]); /// -> Hi there
/// DOES NOT WORK (METHODE 2)
int **int_matrix = malloc(sizeof(int **) * 10);
int_matrix[0][0] = 1; // -> Segmentation fault
/// WORK (METHODE 3)
int **int_matrix2 = malloc(sizeof(int *));
for (int i = 0; i < 10; i++)
{
int_matrix2[i] = malloc(sizeof(int));
}
int_matrix2[0][0] = 42;
printf("%d\n", int_matrix2[0][0]); // -> 42
}
In terms of the types, you want to allocate memory for the type "one level up" from the pointer you're assigning it to. For example, an int pointer (an int*), points to one or more ints. That means, when you allocate space for it, you should allocate based on the int type:
#define NUM_INTS 10
...
int* intPtr = malloc(NUM_INTS * sizeof(int));
// ^^ // we want ints, so allocate for sizeof(int)
In one of your cases, you have a double int pointer (an int**). This must point to one or more int pointers (int*), so that's the type you need to allocate space for:
#define NUM_INT_PTRS 5
...
int** myDblIntPtr = malloc(NUM_INT_PTRS * sizeof(int*));
// ^^ "one level up" from int** is int*
However, there's an even better way to do this. You can specify the size of your object it points to rather than a type:
int* intPtr = malloc(NUM_INTS * sizeof(*intPtr));
Here, intPtr is an int* type, and the object it points to is an int, and that's exactly what *intPtr gives us. This has the added benefit of less maintenance. Pretend some time down the line, int* intPtr changes to int** intPtr. For the first way of doing things, you'd have to change code in two places:
int** intPtr = malloc(NUM_INTS * sizeof(int*));
// ^^ here ^^ and here
However, with the 2nd way, you only need to change the declaration:
int** intPtr = malloc(NUM_INTS * sizeof(*intPtr));
// ^^ still changed here ^^ nothing to change here
With the change of declaration from int* to int**, *intPtr also changed "automatically", from int to int*. This means that the paradigm:
T* myPtr = malloc(NUM_ITEMS * sizeof(*myPtr));
is preferred, since *myPtr will always refer to the correct object we need to size for the correct amount of memory, no matter what type T is.
Others have already answered most of the question, but I thought I would add some illustrations...
When you want an array-like object, i.e., a sequence of consecutive elements of a given type T, you use a pointer to T, T *, but you want to point to objects of type T, and that is what you must allocate memory for.
If you want to allocate 10 T objects, you should use malloc(10 * sizeof(T)). If you have a pointer to assign the array to, you can get the size from that
T * ptr = malloc(10 * sizeof *ptr);
Here *ptr has type T and so sizeof *ptr is the same as sizeof(T), but this syntax is safer for reasons explained in other answers.
When you use
T * ptr = malloc(10 * sizeof(T *));
you do not get memory for 10 T objects, but for 10 T * objects. If sizeof(T*) >= sizeof(T) you are fine, except that you are wasting some memory, but if sizeof(T*) < sizeof(T) you have less memory than you need.
Whether you run into this problem or not depends on your objects and the system you are on. On my system, all pointers have the same size, 8 bytes, so it doesn't really matter if I allocate
char **string_array = malloc(sizeof(char **) * 10);
or
char **string_array = malloc(sizeof(char *) * 10);
or if I allocate
int **int_matrix = malloc(sizeof(int **) * 10);
or
int **int_matrix = malloc(sizeof(int *) * 10);
but it could be on other architectures.
For your third solution, you have a different problem. When you allocate
int **int_matrix2 = malloc(sizeof(int *));
you allocate space for a single int pointer, but you immediately treat that memory as if you had 10
for (int i = 0; i < 10; i++)
{
int_matrix2[i] = malloc(sizeof(int));
}
You can safely assign to the first element, int_matrix2[0] (but there is a problem with how you do it that I get to); the following 9 addresses you write to are not yours to modify.
The next issue is that once you have allocated the first dimension of your matrix, you have an array of pointers. Those pointers are not initialised, and presumably pointing at random places in memory.
That isn't a problem yet; it doesn't do any harm that these pointers are pointing into the void. You can just point them to somewhere else. This is what you do with your char ** array. You point the first pointer in the array to a string, and it is happy to point there instead.
Once you have pointed the arrays somewhere safe, you can access the memory there. But you cannot safely dereference the pointers when they are not initialised. That is what you try to do with your integer array. At int_matrix[0] you have an uninitialised pointer. The type-system doesn't warn you about that, it can't, so you can easily compile code that modifies int_matrix[0][0], but if int_matrix[0] is pointing into the void, int_matrix[0][0] is not an address you can safely read or write. What happens if you try is undefined, but undefined is generally was way of saying that something bad will happen.
You can get what you want in several ways. The closest to what it looks like you are trying is to implement matrices as arrays of pointers to arrays of values.
There, you just have to remember to allocate the arrays for each row in your matrix as well.
#include <stdio.h>
#include <stdlib.h>
int **new_matrix(int n, int m)
{
int **matrix = malloc(n * sizeof *matrix);
for (int i = 0; i < n; i++)
{
matrix[i] = malloc(m * sizeof *matrix[i]);
}
return matrix;
}
void init_matrix(int n, int m, int **matrix)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
matrix[i][j] = 10 * i + j + 1;
}
}
}
void print_matrix(int n, int m, int **matrix)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main(void)
{
int n = 3, m = 5;
int **matrix = new_matrix(n, m);
init_matrix(n, m, matrix);
print_matrix(n, m, matrix);
return 0;
}
Here, each row can lie somewhere random in memory, but you can also put the row in contiguous memory, so you allocate all the memory in a single malloc and compute indices to get at the two-dimensional matrix structure.
Row i will start at offset i*m into this flat array, and index matrix[i,j] is at index matrix[i * m + j].
#include <stdio.h>
#include <stdlib.h>
int *new_matrix(int n, int m)
{
int *matrix = malloc(n * m * sizeof *matrix);
return matrix;
}
void init_matrix(int n, int m, int *matrix)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
matrix[m * i + j] = 10 * i + j + 1;
}
}
}
void print_matrix(int n, int m, int *matrix)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
printf("%d ", matrix[m * i + j]);
}
printf("\n");
}
}
int main(void)
{
int n = 3, m = 5;
int *matrix = new_matrix(n, m);
init_matrix(n, m, matrix);
print_matrix(n, m, matrix);
return 0;
}
With the exact same memory layout, you can also use multidimensional arrays. If you declare a matrix as int matrix[n][m] you will get what amounts to an array of length n where the objects in the arrays are integer arrays of length m, exactly as on the figure above.
If you just write that expression, you are putting the matrix on the stack (it has auto scope), but you can allocate such matrices as well if you use a pointer to int [m] arrays.
#include <stdio.h>
#include <stdlib.h>
void *new_matrix(int n, int m)
{
int(*matrix)[n][m] = malloc(sizeof *matrix);
return matrix;
}
void init_matrix(int n, int m, int matrix[static n][m])
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
matrix[i][j] = 10 * i + j + 1;
}
}
}
void print_matrix(int n, int m, int matrix[static n][m])
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main(void)
{
int n = 3, m = 5;
int(*matrix)[m] = new_matrix(n, m);
init_matrix(n, m, matrix);
print_matrix(n, m, matrix);
int(*matrix2)[m] = new_matrix(2 * n, 3 * m);
init_matrix(2 * n, 3 * m, matrix2);
print_matrix(2 * n, 3 * m, matrix2);
return 0;
}
The new_matrix() function returns a void * because the return type cannot depend on the runtime arguments n and m, so I cannot return the right type.
Don't let the function types fool you, here. The functions that take a matrix[n][m] argument do not check if the matrix has the right dimensions. You can get a little type checking with pointers to arrays, but pointer decay will generally limit the checking. The last solution is really only different syntax for the previous one, and the arguments n and m determines how the (flat) memory that matrix points to is interpreted.
The method 1 works only becuse you assign the char * element of the array string_array with the reference of the string literal `"Hi there". String literal is simply a char array.
Try: string_array[0][0] = 'a'; and it will fail as well as you will dereference not initialized pointer.
Same happens in method 2.
Method 3. You allocate the memory for one int value and store the reference to it in the [0] element of the array. As the pointer references the valid object you can derefence it (int_matrix2[0][0] = 42;)

how do I dereference a pointer which I got from a function in c?

#include <stdio.h>
#include <stdlib.h>
int* CreateArray(int length){
int arr[length];
int i;
for(i=0; i < length; ++i){
arr[i] = -25;
int* ptr = &arr[i];
printf("%p \n",ptr);
printf("%d \n",*ptr);
}
printf("%d \n",arr[0]);
int* location = &arr;
return location;
}
void main()
{
int* arr = CreateArray(10);
printf("pointer here is %p \n",arr);
int num = *arr;
printf("%d",num);
}
when i run the program i want num to be equal to arr[0] which was declared in the function but it doesn't do that for some reason
The array arr is local to the function CreateArray and not marked as static, so it will deleted on returning from the function and cannot be used after that.
To avoid this, you should allocate arrays dynamically.
Also note that &arr is int(*)[length], so it is not good to assign that to a variable of type int*.
int* CreateArray(int length){
int *arr = malloc(sizeof(*arr) * length); /* allocate dynamically */
if (arr == NULL) exit(1); /* check if allocation succeeded */
int i;
for(i=0; i < length; ++i){
arr[i] = -25;
int* ptr = &arr[i];
printf("%p \n",(void*)ptr); /* void* should be passed for %p */
printf("%d \n",*ptr);
}
printf("%d \n",arr[0]);
int* location = arr; /* assign arr, not &arr */
return location;
}
Your function is returning a pointer created inside the function, and it will be deinitialized when the function is popped from the stack. This is called dangling pointer.
If you want to return a pointer, you have to create it on the heap:
int* arr = malloc(sizeof(*arr) * length);;
But pay attention, now you have to manage the garbage!

C, Why does my custom free function give me the "pointer being freed was not allocated" error

I am trying to dynamically allocate an array, put some data in it, and then free it and set the array pointer to null so that it can not be accessed in the future. Also, unrelated, but I am storing the size of the array in the first element and then passing it back indexed one up, it is part of the assignment, so hopefully that doesn't confuse anyone.
If I am understanding the error correctly, I am trying to call free() on the array that my malloc'ed array was copied in to. This is not allowed because free() is not being called on the actual malloc'ed array but rather the one that's holding its values.
If this is the case, how would I fix my call of free() to only receive an array address and dereference it like free(*array);. Right now I have some mess of asteriscs and a cast and I have no idea why it works. If you know how to fix the free call into the above or just explain why what I have now works, I would greatly appreciate it. My goal is to be able to set the parameter for the custom free function to a void pointer instead of a specific data type pointer. Thanks!!
#include <stdlib.h>
int getSizeArray(void *array);
void * createArray(int n, int sizeOfDatatype);
void freeArray(double ** array);
int main(void){
double * arr = createArray(10, sizeof(double));
int size = getSizeArray(arr);
/* using output for error checking
for(int i = 0; i < 10; i++){
arr[i] = i;
}
for(int j = 0; j < 10; j++){
printf("%f\n", arr[j]);
}
*/
void* p = &arr;
freeArray(p);
}
int getSizeArray(void *array){
int s = ((int *) array)[-1];
return s;
}
void * createArray(int n, int sizeOfDatatype){
int * array = malloc((n * sizeOfDatatype) + sizeof(int));
array[0] = n;
return (void*) (array + 1);
}
void freeArray(double ** array){
free(*array);
*array = NULL;
}
EDIT: Look to #JonathanLeffler comment. The issue is with alignment. I switched around some of my code but I had to index back one and not cast in my functions but instead in main
#include <stdlib.h>
int getSizeArray(void *array);
void * createArray(int n, int sizeOfDatatype);
void freeArray(double ** array);
int main(void){
double * arr = createArray(10, sizeof(double));
arr = (void*) (arr + 1);
int size = getSizeArray(arr);
/* using output for error checking*/
for(int i = 0; i < 10; i++){
arr[i] = i;
}
for(int j = 0; j < 10; j++){
printf("%f\n", arr[j]);
}
arr = (double*) (arr - 1);
freeArray(&arr);
for(int j = 0; j < 10; j++){
printf("%f\n", arr[j]);
}
}
int getSizeArray(void *array){
int s = ((int *) array)[-1];
return s;
}
void * createArray(int n, int sizeOfDatatype){
int * array = malloc((n * sizeOfDatatype) + sizeof(int));
array[0] = n;
return array;
}
void freeArray(double ** array){
free(*array);
*array = NULL;
}
I provided a complete solution to this problem for another user. Must be a class assignment. My version is very similar to yours except I used macros instead of functions. Anyway, #Serge answer was so close. It -1 not +1.
Here what I plug into my code and it worked fine:
void freeArray(void** array)
{
free( ((int*)(*array)) - 1 );
*array = NULL;
}
Let me explain what going on. The C allocation routines are basically doing what you are doing. They save the array size one word above the actual array. Follow link for more information on how free() works. In our version, we are saving the array size one int (2 words/4 bytes) above the actual array. Your code was wrong because the address you reference is the 3rd element and not the first. You need to pass in the address where the array allocation originated which is ((int*)(*array)) - 1.
If you free(*array), you don't need to *array = NULL after that.
Also, you can't cast a (void *) onto an (int *) and assign it to a (double *).
Lastly, you can't freeArray(p); if p is a single pointer since freeArray(double ** array) has a parameter of double double-pointer.
Hopefully, this helps.
You can compare my modified code.
#include <stdlib.h>
#include <stdio.h>
int getSizeArray(void *array);
void * createArray(int n, int sizeOfDatatype);
void freeArray(void ** array);
int main(void){
double * arr = (double *)createArray(10, sizeof(double));
int size = getSizeArray(arr);
printf("size of arr %d\n", size);
// using output for error checking
for(int i = 0; i < 10; i++){
arr[i] = i;
}
for(int j = 0; j < 10; j++){
printf("%f\n", arr[j]);
}
void ** p = (void **)&arr;
freeArray(p);
printf("del arr, then arr = %u\n",(unsigned)arr);
}
int getSizeArray(void *array){
int s = ((int *) array)[-1];
return s;
}
void * createArray(int n, int sizeOfDatatype){
int * array = (int*)malloc((n * sizeOfDatatype) + sizeof(int));
array[0] = n;
return (void*) (array + 1);
}
void freeArray(void ** array){
free(((int*)*array)-1);
*array = NULL;
}
output:
size of arr 10
0.000000
1.000000
2.000000
3.000000
4.000000
5.000000
6.000000
7.000000
8.000000
9.000000
del arr, then arr = 0

passing back values from array in struct by ref

struct myStruct
{
int* arr;
int size;
};
void get_back(struct myStruct* my ,int* arr, int* size)
{
arr = my->arr;
*size = my->size;
}
int main()
{
struct myStruct my;
my.arr = (int*) malloc(3 * sizeof(int));
my.arr[0] = 20;
my.arr[1] = 200;
my.arr[2] = 2000;
my.size = 3;
int* ret_arr = NULL;
int size;
get_back(&my, ret_arr, &size);
free(my.arr);
return 1;
}
The goal of my simple program is to get back the values from my.arr into ret_arr, since ret_arr=nullptr, do I need to allocate the memory and than copy it into the array inside get_back function?
or I can just point to the existing array inside "my" struct?
This is my current solution, I copy the values.
struct myStruct
{
int* arr;
int size;
};
int* get_back(struct myStruct* my , int* size)
{
int *arr = (int*)malloc(3 * sizeof(int));
for (int i = 0; i < my->size; i++)
{
arr[i] = my->arr[i];
}
*size = my->size;
return arr;
}
int main()
{
myStruct my;
my.arr = (int*) malloc(3 * sizeof(int));
my.arr[0] = 20;
my.arr[1] = 200;
my.arr[2] = 2000;
my.size = 3;
int* ret_arr = NULL;
int size;
ret_arr = get_back(&my, &size);
free(my.arr);
free(ret_arr);
return 1;
}
Seeing as you're freeing the array, you probably want to copy the contents over with memcpy (from string.h).
You will also need to include stdlib.h for malloc.
#include <string.h>
#include <stdlib.h>
// Struct definition goes here
void get_back(struct myStruct* my, int** arr, int* size)
{
*arr=malloc(my->size*sizeof(int)); //Allocate space for integers
memcpy(*arr, my->arr, my->size*sizeof(int)); //Copy integers to new array
*size=my->size;
}
The function needs to take a pointer to the pointer in order to be able to modify it.
Additionally, your main function will need to be modified too.
int main()
{
struct myStruct my; // Structs are not types.
my.arr = (int*) malloc(3 * sizeof(int));
my.arr[0] = 20;
my.arr[1] = 200;
my.arr[2] = 2000;
my.size = 3;
int* ret_arr = NULL;
int size;
get_back(&my, &ret_arr, &size); //Need to pass ret_arr by reference
free(my.arr);
return 1;
}
Use std::vector, it's very comfortable and has many useful algorithms, there is std::copy function to copy from one vector to another, take a look at your task with a help of vectors:
#include <vector>
#include <iostream>
struct myStruct
{
std::vector<int> arr;
};
int main()
{
myStruct my;
my.arr.push_back(20);
my.arr.push_back(200);
my.arr.push_back(2000);
std::vector<int> ret_arr;
std::copy(my.arr.begin(), my.arr.end(), std::back_inserter(ret_arr));
return 1;
}
And the result is on screen:
If you want to use C language then you should pass pointer on pointer(int** arr) to get right pointing after leaving scope. I will show two methods, first just to point on already allocated memory:
void get_back_pointers(myStruct* my ,int** arr, int* size)
{
*arr = my->arr;
*size = my->size;
}
Other is for deep copy, to allocate new array and copy data to it:
void get_back_copy(myStruct* my ,int** arr, int& size)
{
*arr = (int*) malloc(3 * sizeof(int));
memcpy( *arr, my->arr, my->size * sizeof(int) );
size = my->size;
}
After get_back_copy passed arr will be needed to free its memory. In debugger you can see results that my.arr and ret_arr have one address but ret_arr2 has another because it's allocated in new memory:

C: Having a pointer to pointer in main and create an array in other function

I have code like:
#include <stdio.h>
#include <stdlib.h>
void mad(int ***, int, in);
int main(void) {
int **x;
int n,m;
scanf("%d%d",&n,&m);
mad(x,n,m);
x[0][0] = 5;
printf("%d\n",x[0][0]);
return 0;
}
void mad(int ***x, int n, int m) {
int i;
**x = malloc(sizeof(int *));
for (i = 0; i < n; i++)
***(x + i) = malloc(m * sizeof(int));
}
This is wrong can someone explain why this is wrong and help me to get it right.
You need to have a variable which is a pointer to a pointer, so declare it as such:
int **x;
but if you want another function to assign it a value, you need to pass a pointer to that variable, so declare a function as
void mad(int ***x,int n, int m);
call it with passing a pointer to the variable:
mad(&x,m,n);
and assign a new value to the dereferenced pointer
void mad(int ***x,int n, int m)
{
*x = malloc(...);
}
BTW, the first malloc call seems incorrect – you allocate the block of memory big enough to keep a single pointer to int while it probably should be n pointers to int:
*x = malloc(n*sizeof(int *));
Then allocate your n array's rows, each m ints long:
for (i=0; i<n; i++)
*(*x + i) = malloc(m*sizeof(int));
*x ìs an pointer to a pointer to an array. malloc(n * sizeof(int*)); allocates an array of pointers. *(*x+i) or (*x)[i] is an array. malloc(m * sizeof(int)); allocates an array. Adapt your code like this:
void mad(int ***x, int n, int m){
int i;
*x = malloc(n * sizeof(int*)); // allocate memory where x refers to
for( i=0; i<n; i++ )
*(*x+i) = malloc(m * sizeof(int)); // similar to (*x)[i] = malloc(m * sizeof(int))
}
mad(&x,n,m);
// ^
You should avoid all of those inefficient pointer-to-pointer based lookup tables. They are not arrays since they are not allocated in adjacent memory cells.
Allocate a real 2D array instead:
void alloc2d ( size_t x, size_t y, int(**ptr)[x][y] )
{
*ptr = malloc ( sizeof(int[x][y]) );
}
int main (void)
{
const int x = 5;
const int y = 3;
int (*arr_ptr)[x][y];
alloc2d(x, y, &arr_ptr);
int (*array2d)[y] = *arr_ptr; // point at 1st element which is an array
array2d[i][j] = something;
...
free(arr_ptr);
}

Resources