|
Your donations keep RPGWatch running!
RPGWatch Forums » General Forums » Off-Topic » C - function declearation

Default 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
alenscrist is offline

alenscrist

Banned

#1

Join Date: Jan 2010
Posts: 3
Mentioned: 0 Post(s)

Default 

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] ?
--
"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)
Alrik Fassbauer is offline

Alrik Fassbauer

Alrik Fassbauer's Avatar
TL;DR
Original Sin 1 & 2 Donor

#2

Join Date: Nov 2006
Location: Old Europe
Posts: 20,704
Mentioned: 41 Post(s)

Default 

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].
Last edited by pibbur; January 22nd, 2010 at 19:53.

pibbur

Guest

#3

Posts: n/a
Mentioned: Post(s)

Default 

January 21st, 2010, 18:54
Simply go to the link in the first post and you will see what is "wrong" with the code…
bkrueger is offline

bkrueger

Nothing to see here.

#4

Join Date: Dec 2007
Posts: 1,237
Mentioned: 11 Post(s)

Default 

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
--
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
Myrthos is offline

Myrthos

Myrthos's Avatar
Cave Canem
Administrator
RPGWatch Team

#5

Join Date: Aug 2006
Location: Netherlands
Posts: 10,613
Mentioned: 202 Post(s)

Default 

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.
Last edited by pibbur; January 22nd, 2010 at 19:52.

pibbur

Guest

#6

Posts: n/a
Mentioned: Post(s)

Default 

January 22nd, 2010, 19:36
Originally Posted by pibbur View Post
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:
Originally Posted by alenscrist View Post
display(a, 2, 4); // this line is giving error
should be:
Originally Posted by alenscrist View Post
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:
Originally Posted by alenscrist View Post
p = ptr + i;
should be
Originally Posted by alenscrist View Post
p = ptr[i];
Last edited by noctrun; January 22nd, 2010 at 19:47.
noctrun is offline

noctrun

Watcher

#7

Join Date: Aug 2008
Posts: 59
Mentioned: 0 Post(s)

Default 

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

pibbur

Guest

#8

Posts: n/a
Mentioned: Post(s)

Default 

January 24th, 2010, 17:43
Right, I was wrong as well

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
Myrthos is offline

Myrthos

Myrthos's Avatar
Cave Canem
Administrator
RPGWatch Team

#9

Join Date: Aug 2006
Location: Netherlands
Posts: 10,613
Mentioned: 202 Post(s)

Default 

January 24th, 2010, 17:50
Java saved me from a life of pointers. Thank god for that.
--
"For Innos!"
ToddMcF2002 is offline

ToddMcF2002

ToddMcF2002's Avatar
SasqWatch

#10

Join Date: Oct 2006
Location: Boston MA
Posts: 3,592
Mentioned: 7 Post(s)

Default 

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.

pibbur

Guest

#11

Posts: n/a
Mentioned: Post(s)

Default 

January 24th, 2010, 19:16
But noctrun cheated by using gcc
GothicGothicness is offline

GothicGothicness

GothicGothicness's Avatar
SasqWatch

#12

Join Date: Oct 2006
Posts: 6,233
Mentioned: 12 Post(s)
RPGWatch Forums » General Forums » Off-Topic » C - function declearation

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT +2. The time now is 06:01.
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2022, vBulletin Solutions Inc.
vBulletin Security provided by DragonByte Security (Pro) - vBulletin Mods & Addons Copyright © 2022 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging (Lite) - vBulletin Mods & Addons Copyright © 2022 DragonByte Technologies Ltd.
Copyright by RPGWatch