c - c中数组和指针函数的问题

标签 c arrays function pointers

我试图将一个指向数组的指针传递给 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;  
}

这里是错误: errors

我为什么会得到这个以及如何得到它?

最佳答案

首先您需要在调用函数之前提供每个函数的原型(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/

相关文章:

c - 一对 uint64 值的长乘法

C# 是否可以创建动态 StringBuilder 数组?

javascript - 根据值获取键名列表 - Javascript

c++ - 这是内联函数的有效用法吗?

C:客户端和服务器之间的奇怪行为

c - 需要创建一个函数来调用另一个函数

c - 带有输入变量的函数返回 -nan 和 -inf

java - 查找数组中连续的重复整数

Java 类 - 如何将通用对象传递给函数

Javascript函数不停止提交