#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    12
    Rep 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!!
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,367
    Rep Power
    1870
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    12
    Rep 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!!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    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
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    149
    Rep Power
    36
    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.

IMN logo majestic logo threadwatch logo seochat tools logo