The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
[HELP] Function within function?
Discuss [HELP] Function within function? in the C Programming forum on Dev Shed. [HELP] Function within function? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 8th, 2013, 12:43 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 12
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!!
|

March 8th, 2013, 01:41 AM
|
 |
Contributed User
|
|
|
|
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;
}
|

March 8th, 2013, 02:03 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 12
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!!
|

March 8th, 2013, 12:21 PM
|
|
|
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
|

March 8th, 2013, 02:57 PM
|
 |
Contributing User
|
|
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|