C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 8th, 2013, 12:43 AM
KaiserBreath KaiserBreath is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 12 KaiserBreath User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 58 m 22 sec
Reputation Power: 0
[HELP] Function within function?

Hi,

I am trying to figure out how to use a function within function for a while and it don't seems to work. This is what I got so far:

Code:
typedef struct
{
	char *name;
	int HP, MP;

}charSTAT;

void getStat(charSTAT *stat)
{	
	stat->name="KAISER";
	stat->HP=1000;
	stat->MP=500;
}

void printStat(void(getStat)(charSTAT *a))
{

	getStat(*a);
	printf("%s", a->name);
}


int main()
{
	charSTAT Kaiser;		//define the struct
	printStat(getStat(&Kaiser));	//using getStat function, input data into struct Kaiser and print it out.	


	return 0;
}


Please advice, thanks!!

Reply With Quote
  #2  
Old March 8th, 2013, 01:41 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,839 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 1 m 4 sec
Reputation Power: 1774
Eg.
Code:
charSTAT *getStat(charSTAT *stat)
{	
	stat->name="KAISER";
	stat->HP=1000;
	stat->MP=500;
	return stat;
}

void printStat(charSTAT *a)
{
	printf("%s", a->name);
}

int main()
{
	charSTAT Kaiser;		//define the struct
	printStat(getStat(&Kaiser));	//using getStat function, input data into struct Kaiser and print it out.	


	return 0;
}
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old March 8th, 2013, 02:03 AM
KaiserBreath KaiserBreath is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 12 KaiserBreath User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 58 m 22 sec
Reputation Power: 0
Thanks for the reply! But I wanted to try using a function within a function. I finally foud out the correct syntax using trial and error after a long time:

Code:
typedef struct
{
	char *name;
	int HP, MP;

}charSTAT;

void getStat(charSTAT *stat)
{	
	stat->name="KAISER";
	stat->HP=1000;
	stat->MP=500;
}

void printStat(void(*getStat)(void *a), charSTAT *a)
{

	getStat(a);                         //I want to use function in a function
	printf("%s", a->name);
}


int main()
{
	charSTAT Kaiser;		//define the struct
	printStat(getStat, &Kaiser);	//using getStat function, input data into struct Kaiser and print it out.	


	return 0;
}


Thanks anyway!!

Reply With Quote
  #4  
Old March 8th, 2013, 12:21 PM
jimblumberg jimblumberg is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 42 jimblumberg User rank is Sergeant (500 - 2000 Reputation Level)jimblumberg User rank is Sergeant (500 - 2000 Reputation Level)jimblumberg User rank is Sergeant (500 - 2000 Reputation Level)jimblumberg User rank is Sergeant (500 - 2000 Reputation Level)jimblumberg User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 4 h 9 m 24 sec
Reputation Power: 18
You don't need to add the function to the parameters just call the function.

Code:
void getStat(charSTAT *stat)
{	
	stat->name="KAISER";
	stat->HP=1000;
	stat->MP=500;
}

void printStat(charSTAT *a)
{

	getStat(a);                         //I want to use function in a function
	printf("%s", a->name);
}


Jim

Reply With Quote
  #5  
Old March 8th, 2013, 02:57 PM
jakotheshadows's Avatar
jakotheshadows jakotheshadows is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 149 jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level)jakotheshadows User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 12 h 1 m 16 sec
Reputation Power: 35
Your problem was you were passing in a charSTAT by value with getStat(*a).

In the context of function arguments, as opposed to parameters (read up on the difference), when you place * next to a pointer type as you did when you called getStat it is the dereference operator. a is already a pointer to a charStat, as you specify in your function parameters. Therefore when you pass it into a function as *a you dereference that pointer and pass in a charStat by value instead of passing it the pointer you specify that getStat takes.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > [HELP] Function within function?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap