使用c中的指针将2d数组传递给函数

时间:2014-03-29 21:52:08

标签: c function pointers multidimensional-array

这是我第一次不得不在这里问一个通常不需要的问题,但这次我无法弄明白。 基本上我试图在控制台中创建一个1和0(随机位置为1和0)的网格。 1s是木屑,0s代表空白。 要做到这一点,我必须使用此函数初始化和填充二维数组并返回指向已完成数组的指针

int* createEnvironment(int width, int height, int numWoodChips)
然后我需要使用此函数打印2d数组

void printEvrionment(int* environment, int width, int height)

这是我到目前为止所拥有的

int enviroWidth = 20;
int enviroHeight = 20;
int* createEnvironment(int width, int height, int numWoodChips);
void printEvrionment(int* environment, int width, int height);
void freeArray(int width, int* environment);
int main(int argc, char **argv)
{

int *arr = createEnvironment(enviroWidth, enviroHeight, 10);
printEvrionment(arr, enviroWidth, enviroHeight);

getchar();
freeArray(enviroHeight, arr);
return 0;
}

int* createEnvironment(int width, int height, int numWoodChips)
{
int Num = 1;
/*randNum = (rand() % 1) + 0;*/
int temp = 3;
int woodChipCounter = 0;
int counter = 0;
int *array;
array = malloc(sizeof(array)*height *width);



for (int j = 0; j < height; j++)
{
    for (int i = 0; i < width; i++)
    {
        counter++;
        if (counter == temp)
        {
            array[j*width + i] = 1;
            temp++;
            woodChipCounter++;
            counter = 0;
        }
        else
        {
            array[j*width + i] = 0;
        }

        if (woodChipCounter == numWoodChips)
        {
            Num = 0;
        }
    }
}
return array;
}

void printEvrionment(int* environment, int width, int height)
{
for (int j = 0; j < height; j++)
{
    printf("\n");
    for (int i = 0; i < width; i++)
    {
        printf("%d", environment[j]);

    }
}
}

void freeArray(int height, int* environment)
{
for (int i = 0; i < height; i++)
{
    free(environment[i]);
}
free(environment);
}

1 个答案:

答案 0 :(得分:0)

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

int enviroWidth = 20;
int enviroHeight = 20;

char *createEnvironment(int width, int height, int numWoodChips);
void printEvrionment(char *environment, int width, int height);
//void freeArray(int width, int* environment);

int main(int argc, char **argv){
    char *arr = createEnvironment(enviroWidth, enviroHeight, 10);

    printEvrionment(arr, enviroWidth, enviroHeight);

    getchar();
    //freeArray(enviroHeight, arr);
    free(arr);
    return 0;
}

char *createEnvironment(int width, int height, int numWoodChips){
    srand(time(NULL));
    int len = width * height;
    char *array = malloc(len);

    if(numWoodChips > len)
        numWoodChips = len;

    memset(array, '1', numWoodChips);
    memset(array + numWoodChips, '0', len - numWoodChips);

    for(int i=0;i<numWoodChips; ++i){
        char tmp;
        int pos = rand()%len;
        //swap array[i] and array[pos]
        tmp = array[i];
        array[i] = array[pos];
        array[pos] = tmp;
    }

    return array;
}

void printEvrionment(char *environment, int width, int height){
    printf("\n");
    for (int j = 0; j < height; ++j){
        for (int i = 0; i < width; ++i){
            printf("%c", environment[width * j + i]);
        }
        printf("\n");
    }
}