C - function declearation

alenscrist

Banned
Joined
January 21, 2010
Messages
3
Hi Guys,

I have written the program for accesssing elements of 2D array using pointer to array of int which is as below:

void display(int (*)[4], int , int);

int main()
{
int a[2][4] = {{1,2,3,4},
{5,6,7,8}
};

display(a, 2, 4); // this line is giving error

getch();
}

void display(int (*ptr)[4], int row, int col)
{

int i, j;
int *p;

for(i = 0; i < row; i++)
{
p = ptr + i;
for( j= 0; j < col; j++)
{
printf("%d ", *(p+j));
}
printf("\n");
}
}

But the function declearion line is giving the error and also I am not sure about function prototype written correct or not.

Please help me in how the function prototype and declearation has to be written for the above fucntion defination.

Thanks
 
Joined
Jan 21, 2010
Messages
3
display(a, 2, 4); // this line is giving error

Are 2 and 4 values or shouldn't they rather be [2] [4] ?
 
Joined
Nov 5, 2006
Messages
21,908
Location
Old Europe
EDIT: Meaningless drivel - see post below

display(a, 2, 4); // this line is giving error

The above line is a call of the display function, and that is the error. You are calling the function outside main() or a function called by main(). You can't do that. For instance, the array a is declared inside main() and is not defined/visible outside.

2 and 4 are correct int argument values, and should not be passed as [2] and [4].
 
Last edited:
Simply go to the link in the first post and you will see what is "wrong" with the code…:cm:
 
Joined
Dec 26, 2007
Messages
1,786
As the link is gone the only thing wrong now is that it should be: display(a, 1, 3);

C is zero-based.

Great we are actually getting some fun time with spam bots :)
 
Joined
Aug 30, 2006
Messages
11,223
Sorry, but display(a,2,4) is correct, because the loops within the functions are

for(i = 0; i < row; i++) //which traverse elements 0,1
{
for ( j= 0; j < col; j++) //which traverse elements 0,1,2,3

EDIT: Meaningless drivel coming - see post below
It's still incorrect to call dispaly(…) outside main() or functions called from main(). The compiler will complain because the array a is used outside its scope.
 
Last edited:
It's still incorrect to call dispaly(…) outside main() or functions called from main(). The compiler will complain because the array a is used outside its scope.
No it would complain that you can't call a function outside a function, but that is not the case in the above source code. don't let the array declaration fool you. There is a missing line "#include <stdio.h>" and "getch();" should read "return 0". other than that there are only two warnings in that code on gcc -pendanic (ANSI C) the first one is the C++ style comment:
display(a, 2, 4); // this line is giving error
should be:
display(a, 2, 4); /* this line is giving error */
even though that comment is out of date. The second is assignment from incompatible pointer type:
p = ptr + i;
should be
 
Last edited:
Joined
Aug 5, 2008
Messages
59
Ooops! This is embarassing - I overlooked the number of }'s and {'s. Thus my claim that display(..) was called out of scope. Which is BS!

Sorry!

You may kick my behind!!

I shall not comment on "errors" without chekcing thoroughly
I shall not comment on "errors" without chekcing thoroughly
I shall not comment on "errors" without chekcing thoroughly
I shall not comment on "errors" without chekcing thoroughly
I shall not comment on "errors" without chekcing thoroughly
I shall not comment on "errors" without chekcing thoroughly
I shall not comment on "errors" without chekcing thoroughly
I shall not comment on "errors" without chekcing thoroughly
I shall not comment on "errors" without chekcing thoroughly
I shall not comment on "errors" without chekcing thoroughly
 
Right, I was wrong as well :)

Noctrun is the winner :party:
 
Joined
Aug 30, 2006
Messages
11,223
Hah! Pointers are fun! Deadly, but fun!

At work we use C# and .net. But when I program for fun, it's C/C++ and either XWindows or Win32 APIs.
 
Back
Top Bottom