Search code examples
cpointersparametersreturn-typefunction-declaration

In there any difference between *<variable> and <variable>* in C?


in the following function declaration

my_struct* create(char *name);

Is there any difference in the meaning of * in my_struct* and *name?

I understand that char *name means that we will pass a char pointer to the function my_struct called name. I also get that the function will return a pointer to (memory address of) something. What I don't understand is why my_struct* and not *my_struct?


Solution

  • In this declaration

    my_struct * create(char *name);
    

    the name of the function is create. The function has one parameter of the type char *, And the function has the return type my_struct *. That is it returns a pointer to an object of the type my_struct.

    Is there any difference in the meaning of * in my_struct* and *name

    my_struct is a type specifier. So my_struct * is a pointer type. name is identifier that denotes the name of a parameter. The type of the parameter (identifier) name is char *.

    Pay attention to that these declarations of the parameter are the same

    char* name
    char * name
    char *name
    

    that is the type of the parameter is char * and the name of the parameter is name.

    In a function declaration that is not at the same time its definition names of parameters may be omitted.

    So the above function declaration can be also written like

    my_struct * create( char * );