简体   繁体   中英

difference between function pointer and function name

What is a function name? What's its relation to a pointer to it?To try to understand these questions, codes below are written:

#include <stdio.h>

int testFunc(void);

void ptrFuncProp(void);

int main(){
    ptrFuncProp();
    return 0;
}

void ptrFuncProp(void){
    int i = 0;
    int (*p_testFunc)(void) = testFunc;

    testFunc();
    (*testFunc)();
    p_testFunc();
    (*p_testFunc)();

    printf("testFunc:\t%d\n*testFunc:\t%d\n",sizeof(testFunc),sizeof(*testFunc));
    printf("p_testFunc:\t%d\n*p_testFunc:\t%d\n",sizeof(p_testFunc),sizeof(*p_testFunc));
    putchar('\n');

    printf("testFunc:\t%c\n",testFunc);
    printf("*testFunc:\t%c\n",*testFunc);
    printf("*p_testFunc:\t%c\n",*p_testFunc);

    for(;*p_testFunc && i<30;i++){
        printf("%c ",*(p_testFunc + i));
        if(i%10 == 9){
            putchar('\n');
        }
    }
}

int testFunc(void){
    int i=0;
    printf("output by testFunc\n");
    return 0;
}

The output is as follows:

output of the program

In the code, a simple function testFunc is defined, and a pointer p_testFunc points to it.As I learned on the internet, I tried four ways to call this function;they all work although I don't exactly understand.

Next 2 lines try to figure out what really are the function name and a pointer of it.One thing I can understand is that p_testFunc is a pointer, so it contains the address of something else;the address is 8 bytes. But why the size of the function name is 1 bytes because I used to think a function name is a const pointer whose content is the address of the start of the function. If the function name is not a pointer, how can it be dereferenced?

After the experiment, the questions are still unsolved.

If you just got into C you should grasp at what pointer is first.

" A pointer is a variable whose value is the address of another variable, ie, direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address ".

There is not differece between pointer to a integer/char etc. and a pointer to a function. Its purpose is to point to an address in the memory where ,in this case, the function is stored.

Name of the function on other hand is just how the functions is named. As people suggested in the comments it identify the function in front of the compiler,linker.

how a function is defined:

int ( what the function will return) isEven (the function name) (int number) ( what argument will it accept)
//How it would look like
int isEven (int number){

   //Here goes the body!

}

Just a little overview of the function.

How is the pointer function defined:

int (return type) *(*isEven(Name))(int(input arguments));
//No tips again!
int  (*isEven)(int);

Also I noticed in your code you didnt use any &. Consider the result of the following snipet:

    #include <stdio.h>
void my_int_func(int x)
{
    printf( "%d\n", x );
}

int main()
{
    void (*foo)(int);
    /* the ampersand is actually optional */
    foo = &my_int_func;
    printf("addres: %p \n", foo);
    printf("addres: %p \n",  my_int_func);


    return 0;
}

Note: %p will format you the addresses of what is inputed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM