我试图将一个指向数组的指针传递给 2 个函数,但出现了一堆错误。 这是我的代码的一部分:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
struct tetromino {
int color;
int location[4][2];
};
int main()
{
int page[22][18]={0};
struct tetromino block[7][4];
block[1][0].color=13;
block[1][0].location[0][0]=9;
block[1][0].location[0][1]=-2;
block[1][0].location[1][0]=9;
block[1][0].location[1][1]=-1;
block[1][0].location[2][0]=8;
block[1][0].location[2][1]=0;
block[1][0].location[3][0]=9;
block[1][0].location[3][1]=0;
tet_effect(page,block[1][0]);
print(page);
}
void tet_effect(int *page,struct tetromino block)
{
int i;
for(i=0;i<4;i++)
{
page[block.location[i][0]][block.location[i][1]]=block.color;
}
return;
}
void print(int *page)
{
int i,j;
system("cls");
for (i=0;i<22;i++)
{
printf("|");
for (j=0;j<18;j++)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),page[i][j]);
printf("#");
}
printf("|\n");
}
printf("|");
for (j=0;j<18;j++)
{
printf("-");
}
printf("|");
return;
}
我为什么会得到这个以及如何得到它?
最佳答案
首先您需要在调用函数之前提供每个函数的原型(prototype)。
那么你需要把参数int *page
改成int (*page)[18]
因为当page
在 main
传递给这些函数,然后它将转换为指向数组 page
的第一个元素的指针。
由于 array
是数组的数组 类型,因此它的第一个元素是数组。因此,array
将被转换为指向数组类型的指针,即 int (*)[18]
。
原型(prototype)应该是
void tet_effect(int (*page)[18], struct tetromino block);
void print(int (*page)[18]);
关于c - c中数组和指针函数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34449040/