Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create 2D array by passing pointer to function in c

So I read dozens of examples of passing an 2D array pointer to function to get/change values of that array in function. But is it possible to create (allocate memory) inside the function. Something like this:

#include <stdio.h>

void createArr(int** arrPtr, int x, int y);

int main() {

    int x, y;       //Dimension
    int i, j;       //Loop indexes
    int** arr;      //2D array pointer
    arr = NULL;
    x=3;
    y=4;

    createArr(arr, x, y);

    for (i = 0; i < x; ++i) {
        for (j = 0; j < y; ++j) {
            printf("%d\n", arr[i][j]);
        }
        printf("\n");
    }
    _getch();    
}

void createArr(int** arrPtr, int x, int y) {
    int i, j;       //Loop indexes
    arrPtr = malloc(x*sizeof(int*));
    for (i = 0; i < x; ++i)
        arrPtr[i] = malloc(y*sizeof(int));

    for (i = 0; i < x; ++i) {
        for (j = 0; j < y; ++j) {
            arrPtr[i][j] = i + j;
        }
    }    
}
like image 628
Marcin Avatar asked Jun 23 '26 22:06

Marcin


2 Answers

Forget about pointer-to-pointers. They have nothing to do with 2D arrays.

How to do it correctly: How do I correctly set up, access, and free a multidimensional array in C?.

One of many reasons why it is wrong to use pointer-to-pointer: Why do I need to use type** to point to type*?.

Example of how you could do it properly:

#include <stdio.h>
#include <stdlib.h>


void* create_2D_array (size_t x, size_t y)
{
  int (*array)[y] = malloc( sizeof(int[x][y]) );

  for (size_t i = 0; i < x; ++i) 
  {
    for (size_t j = 0; j < y; ++j) 
    {
      array[i][j] = (int)(i + j);
    }
  }

  return array;
}

void print_2D_array (size_t x, size_t y, int array[x][y])
{
  for (size_t i = 0; i < x; ++i) 
  {
    for (size_t j = 0; j < y; ++j) 
    {
      printf("%d ", array[i][j]);
    }
    printf("\n");
  }
}


int main (void)
{
  size_t x = 5;
  size_t y = 3;

  int (*arr_2D)[y];

  arr_2D = create_2D_array(x, y);

  print_2D_array(x, y, arr_2D);

  free(arr_2D);

  return 0;
}
like image 131
Lundin Avatar answered Jun 26 '26 14:06

Lundin


Yes, passing a pointer to int ** (but 3 stars is considered bad style), I suggest to return an allocated variable from your function:

int **createArr(int x, int y)
{
    int **arrPtr;
    int i, j;       //Loop indexes

    arrPtr = malloc(x*sizeof(int*));
    if (arrPtr == NULL) { /* always check the return of malloc */
        perror("malloc");
        exit(EXIT_FAILURE);
    }
    for (i = 0; i < x; ++i) {
        arrPtr[i] = malloc(y*sizeof(int));
        if (arrPtr[i] == NULL) {
            perror("malloc");
            exit(EXIT_FAILURE);
        }
    }
    for (i = 0; i < x; ++i) {
        for (j = 0; j < y; ++j) {
            arrPtr[i][j] = i + j;
        }
    }
    return arrPtr;   
}

Call it using:

arr = createArr(x, y);
like image 38
David Ranieri Avatar answered Jun 26 '26 14:06

David Ranieri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!