0

I'm trying to pass a pointer to an array to 2 functions and I'm getting a bunch of errors. Here's a part of my code:

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

And here are the errors: errors

Why am I getting this and how do I it?

AdrieanKhisbe
  • 3,899
  • 8
  • 37
  • 45
  • No, i'm not clicking on the link. – Sourav Ghosh Dec 24 '15 at 07:38
  • To begin with, where is the *prototype declaration* of the `tet_effect` function`? To continue, [a pointer to a pointer to something is not the same as a an array of array of something](http://stackoverflow.com/a/18440456/440558). – Some programmer dude Dec 24 '15 at 07:42

3 Answers3

2

First you need to provide the prototype of each function before calling them.

Then you need to change the parameter int *page to int (*page)[18] because when page in main passed to these functions then it will converted to pointer to the first element of array page.
Since array is of type an array of arrays, therefore its first element is an array. So, array will be converted to pointer to array type, i.e int (*)[18].

Prototype should be

void tet_effect(int (*page)[18], struct tetromino block);
void print(int (*page)[18]); 
haccks
  • 104,019
  • 25
  • 176
  • 264
1

Please be aware that in your environment, you are using one of Microsoft's compilers, which resembles a C89 implementation.

In answering your questions, I will refer to the line-numbers as mentioned in the warnings and errors you've shared a picture of, as opposed to the line-numbers of the code you've shared. (They are different.)

First warning (orange):

When you declare and define tet_effect on line 27, you define its type. But the fact that it has also appeared before that definition (on line 22) means that it has already been deemed to have been declared to have the type int(). (A function taking an unspecified number of arguments and returning an int value.)

First error (red):

On line 32 you have:

page[block.location[i][0]][block.location[i][1]]=block.color;

The type of page is int *, as declared on line 27. Thus, the type of page[anything] is int. An int is not an array nor a pointer, so the subsequent [block.location[i][1]] is not valid.

Second warning (orange):

This is the same kind of error as the earlier warning. You attempt to declare and define print on line 36, but you'd accidentally declared it to have type int() on line 23, by calling it.

Second error (red):

This is the same kind of error as the earlier error. The type of page is int *, so the type of page[i] is int, then the subsequent [j] is an error because an int is not an array.

synthetel
  • 63
  • 5
0

Two things to mention here...

  1. You need to have a forward declaration of your functions. Without a forward declaration your compiler will not be able to know the function prototypes and as per the latest C11 standard, implicit declaration of functions are not a part of the standard C anymore.

  2. You pass the page as an int *, so later you cannot do page[i][j], because, page[i] will give you an int, anyways.

That said, in your main(), page is of type int [22][18], so you may not pass that to a simple int *, you will be needing the function parameter to be of matching type, something like (int Innerpage[][18]) or something simmilar.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261