|
Your donations keep RPGWatch running!
C - function declearation
January 21st, 2010, 12:47
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
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
Banned
January 21st, 2010, 13:11
display(a, 2, 4); // this line is giving error
Are 2 and 4 values or shouldn't they rather be [2] [4] ?
Are 2 and 4 values or shouldn't they rather be [2] [4] ?
--
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius and a lot of courage to move in the opposite direction." (E.F.Schumacher, Economist, Source)
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius and a lot of courage to move in the opposite direction." (E.F.Schumacher, Economist, Source)
January 21st, 2010, 13:24
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].
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 by pibbur; January 22nd, 2010 at 19:53.
Guest
January 21st, 2010, 18:54
Simply go to the link in the first post and you will see what is "wrong" with the code…
Nothing to see here.
January 22nd, 2010, 19:05
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
C is zero-based.
Great we are actually getting some fun time with spam bots
--
In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move. Douglas Adams
There are no facts, only interpretations. Nietzsche
Some cause happiness wherever they go; others whenever they go. Oscar Wilde
In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move. Douglas Adams
There are no facts, only interpretations. Nietzsche
Some cause happiness wherever they go; others whenever they go. Oscar Wilde
January 22nd, 2010, 19:16
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.
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 by pibbur; January 22nd, 2010 at 19:52.
Guest
January 22nd, 2010, 19:36
Originally Posted by pibburNo 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:
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.
Originally Posted by alenscristshould be:
display(a, 2, 4); // this line is giving error
Originally Posted by alenscristeven though that comment is out of date. The second is assignment from incompatible pointer type:
display(a, 2, 4); /* this line is giving error */
Originally Posted by alenscristshould be
p = ptr + i;
Originally Posted by alenscrist
p = ptr[i];
Last edited by noctrun; January 22nd, 2010 at 19:47.
Watcher
January 22nd, 2010, 19:51
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
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
Guest
January 24th, 2010, 17:43
Right, I was wrong as well 
Noctrun is the winner

Noctrun is the winner
--
In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move. Douglas Adams
There are no facts, only interpretations. Nietzsche
Some cause happiness wherever they go; others whenever they go. Oscar Wilde
In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move. Douglas Adams
There are no facts, only interpretations. Nietzsche
Some cause happiness wherever they go; others whenever they go. Oscar Wilde
January 24th, 2010, 17:50
Java saved me from a life of pointers. Thank god for that.
--
"For Innos!"
"For Innos!"
January 24th, 2010, 18:58
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.
At work we use C# and .net. But when I program for fun, it's C/C++ and either XWindows or Win32 APIs.
Guest
|
|
All times are GMT +2. The time now is 06:01.
