GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Mar-2004, 05:41
spudtheimpaler spudtheimpaler is offline
Junior Member
 
Join Date: Feb 2004
Posts: 46
spudtheimpaler is on a distinguished road

Calling functions within a function


Hi,

I was under the impression you can call a function from within a function. (hell recursions calling the same function within a function) so why am i getting errors with the code (attatched, cause its pretty big)? Its the status function that i've highlighted, i'm getting compiler errors regarding the "definition" of the function. I imagine it is the arguments to the function (I'm still pretty new, so pointers are not my forrté)

Any help would be greatly appreciated

Mitch
Attached Files
File Type: txt code.txt (4.5 KB, 140 views)
  #2  
Old 02-Mar-2004, 07:29
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi spud. There a couple of problems that I see. First of all your functions need to be defined prior to calling them. So you either need to move your status function above your move function or do a definition at the top of your file like:
CPP / C++ / C Code:
void status(rover *, location **);

It is actually not a bad idea to do this, maybe even in a seperate header file for all of your functions (except main )

Also, when you call status in this context:
CPP / C++ / C Code:
void move(rover *myRover, location **map)
{

	...

	printf("%d %d", myRover->Current_x, myRover->Current_y);
	status(&myRover, map); //This is where i seem to be getting an error

}

You are passing the address of myRover, when you actually should just pass myRover.

HTH
  #3  
Old 02-Mar-2004, 07:47
spudtheimpaler spudtheimpaler is offline
Junior Member
 
Join Date: Feb 2004
Posts: 46
spudtheimpaler is on a distinguished road
Quote:
Originally Posted by dsmith
Hi spud. There a couple of problems that I see. First of all your functions need to be defined prior to calling them. So you either need to move your status function above your move function or do a definition at the top of your file like:
CPP / C++ / C Code:
void status(rover *, location **);

It is actually not a bad idea to do this, maybe even in a seperate header file for all of your functions (except main )

Also, when you call status in this context:
CPP / C++ / C Code:
void move(rover *myRover, location **map)
{

	...

	printf("%d %d", myRover->Current_x, myRover->Current_y);
	status(&myRover, map); //This is where i seem to be getting an error

}

You are passing the address of myRover, when you actually should just pass myRover.

HTH


Cheers!

It doesnt like my definition is there a #something i have to put before it? and i vageuly remember being told about this definition thing. A header file seems like a very good idea, with all the function declarations, but do they have to be a certain format or can they just be a list of function declarations..

I'll play with headers one day.

I have tried the declarati you said, and i cant see anything wrong with it, it returns nothign so the void is fair, and the rover *, location ** seems right.

I swear lately C has driven me almost into using face.... though not yet.

Thank you for your reply dsmith, its appreciated.

Mitch
  #4  
Old 02-Mar-2004, 07:51
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Mitch. What is the error you are getting?

Maybe try:
CPP / C++ / C Code:
void status(rover *myRover, location **map);

My compiler doesn't need the variable names, but maybe yours does.

Also, make sure this definition is below the declaration of your structures for location and rover. I think I told you to put it at the top of the file

HTH
  #5  
Old 02-Mar-2004, 07:59
spudtheimpaler spudtheimpaler is offline
Junior Member
 
Join Date: Feb 2004
Posts: 46
spudtheimpaler is on a distinguished road
Thats prob what it is then, i have put it just under the #includes...
It compiles now, so thank you!

also, a side note: is this allowed:

Code:
printf("%d ",myRover.map[myRover.Current_x][myRover.Current_y].ObjId);


Its also giving me hassle and not letting me carry on with the bits i need to.



The full code is above, but basically myRover is a structure containing a current x and current y and a map. the map is a 2d array of structures, with each "location" containing information such as an object id (such as 1=rock, 2=pool)

i'm trying to access the object id, based on only knowing the current_x and current_y position,

know what I'm getting at?

Any help again would be greatly appreciated.

Mitch
  #6  
Old 02-Mar-2004, 08:02
spudtheimpaler spudtheimpaler is offline
Junior Member
 
Join Date: Feb 2004
Posts: 46
spudtheimpaler is on a distinguished road
Thumbs up

Quote:
Originally Posted by spudtheimpaler
Thats prob what it is then, i have put it just under the #includes...
It compiles now, so thank you!

also, a side note: is this allowed:

Code:
printf("%d ",myRover.map[myRover.Current_x][myRover.Current_y].ObjId);


Its also giving me hassle and not letting me carry on with the bits i need to.



The full code is above, but basically myRover is a structure containing a current x and current y and a map. the map is a 2d array of structures, with each "location" containing information such as an object id (such as 1=rock, 2=pool)

i'm trying to access the object id, based on only knowing the current_x and current_y position,

know what I'm getting at?

Any help again would be greatly appreciated.

Mitch

you know when you read the preview, looks fine, post then read the actual post and answer your own question...

skip the myRover bit, yeah i thought so

Cheers for your help dsmith, really sorted me out!

Thanks again, Mitch
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
run script command on ns2.26 newbie06 Computer Software Forum - Linux 53 23-May-2008 23:36
Flex and bison coding lucky88star C++ Forum 5 24-Dec-2007 11:57
Need Help with input files. Efferus C++ Forum 2 24-Nov-2007 16:19
Arrays as function arguments - return arrays from functions pisuke C Programming Language 2 25-Jul-2007 11:03
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:57.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.