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

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0

    Taking a function as a parameter


    Why does this code print random characters and not my inputs? (Compiles, runs, just doesn't do what I want to do)

    Code:
    #include<stdio.h>
    #include<string.h>
    
    char *initials(char first[],char middle,char last[])
    {
    	char name[] = "x";
    	int length = strlen(first) + strlen(last) + 2;
    	name[length];
    	strcpy(name,first);
    	name[strlen(first) + 1] = middle;
    	strcat(name,last);
    	return name;
    }
    
    char *birthName(char*(*function)(char *,char,char *))
    {
    	return function("Chuck",'S',"Norris");
    }
    
    int main()
    {
    	printf("%s\n",birthName(initials));
    	getchar();
    	return 0;
    }
    I want it to print ChuckSNorris but instead just prints a bunch of random character. I want it to run by the function, birthName, taking a parameter as a function.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    I'm more used to separating things out to avoid possible confusion. Our SOP is to create function pointer types with typedef and then use that new type declarations. But that's not what is cause of the problem is.

    Code:
    char *initials(char first[],char middle,char last[])
    {
    	char name[] = "x";
    	int length = strlen(first) + strlen(last) + 2;
    	name[length];
    	strcpy(name,first);
    	name[strlen(first) + 1] = middle;
    	strcat(name,last);
    	return name;
    }
    When you return from the function, what is the state of name? It's a local array variable, so it was created on the stack along with the other local variables. When you return from the function, all local variables and other housekeeping data for the function get popped off the stack and cease to exist -- conceptually, but in reality those memory locations get reused. As a result, we must never return a pointer to a local variable.

    Declare name as static. That will cause it to persist between function calls, because it will be in static storage rather than auto.

    But you declared name as a two-character array, so you will overflow that buffer when you try to store more than one readable character in it. I'm surprised your program doesn't crash.

    Unless you're pulling some weird C99 crap in which case you should have warned us that you're using something out-of-the-ordinary.
    Last edited by dwise1_aol; April 20th, 2013 at 12:09 PM.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480

    Also


    function is a pointer, which surprisingly you managed to declare correctly. Upon using that pointer, you should dereference it.
    Code:
    char *birthName(char*(*function)(char *,char,char *))
    {
    	return (*function)("Chuck",'S',"Norris");
    }
    The modern compilers may cover your hind quarters in this case despite that I think they should not.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo