简体   繁体   English

通过将指针传递给c中的函数来创建2D数组

[英]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. 所以我读了几十个将2D数组指针传递给函数的例子,以便在函数中获取/更改该数组的值。 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;
        }
    }    
}

Forget about pointer-to-pointers. 忘记指针指针。 They have nothing to do with 2D arrays. 它们与2D阵列无关。

How to do it correctly: How do I correctly set up, access, and free a multidimensional array in C? 如何正确地做到这一点: 如何在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;
}

Yes, passing a pointer to int ** (but 3 stars is considered bad style), I suggest to return an allocated variable from your function: 是的,将指针传递给int ** (但是3星被认为是坏的样式),我建议从函数返回一个已分配的变量:

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);

Yes, an array can be initialized this way. 是的,可以通过这种方式初始化数组。 As long as you pass a pointer, the memory address should remain the same. 只要传递指针,内存地址应保持不变。 So, if you assign anything to the pointer it will valid. 因此,如果您为指针指定任何内容,它将有效。

Think of an a[] as a* pointer to the first element 将a []视为指向第一个元素的*指针

a [][] will be a** pointer to a pointer of the first element or a pointer to the first array (first row of a table) a [] []将是指向第一个元素的指针的**指针或指向第一个数组的指针(表的第一行)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM