通过传递函数指针在C创建二维数组 [英] Create 2D array by passing pointer to function in c

查看:197
本文介绍了通过传递函数指针在C创建二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我看了几十传递一个二维数组指针的例子函数获取函数数组/变化值。但是,它可以创建(分配内存)的函数中。事情是这样的:

 的#include<&stdio.h中GT;无效createArr(INT ** arrPtr,诠释的x,int y)对;诠释主(){    INT X,Y; //尺寸
    INT I,J; //循环指标
    INT ** ARR; //二维数组指针
    ARR = NULL;
    X = 3;
    Y = 4;    createArr(ARR,X,Y);    对于(i = 0; I< X ++我){
        为(J = 0; J< Y ++ j)条{
            的printf(%d个\\ N,编曲[I] [J]);
        }
        的printf(\\ n);
    }
    _getch();
}无效createArr(INT ** arrPtr,诠释的x,int y)对{
    INT I,J; //循环指标
    arrPtr =的malloc(X *的sizeof(INT *));
    对于(i = 0; I< X ++ I)
        arrPtr [I] =的malloc(Y *的sizeof(INT));    对于(i = 0; I< X ++我){
        为(J = 0; J< Y ++ j)条{
            arrPtr [I] [J] = I + J;
        }
    }
}


解决方案

是的,指针传递到 INT ** (但3星被认为是不好的风格),我建议从你的函数返回一个分配的变量:

  INT ** createArr(INT X,int y)对
{
    INT ** arrPtr;
    INT I,J; //循环指标    arrPtr =的malloc(X *的sizeof(INT *));
    如果(arrPtr == NULL){/ *经常检查的malloc返回* /
        PERROR(malloc的);
        出口(EXIT_FAILURE);
    }
    对于(i = 0; I< X ++我){
        arrPtr [I] =的malloc(Y *的sizeof(INT));
        如果(arrPtr [I] == NULL){
            PERROR(malloc的);
            出口(EXIT_FAILURE);
        }
    }
    对于(i = 0; I< X ++我){
        为(J = 0; J< Y ++ j)条{
            arrPtr [I] [J] = I + J;
        }
    }
    返回arrPtr;
}

使用调用它:

  ARR = createArr(X,Y);

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

解决方案

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

这篇关于通过传递函数指针在C创建二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆