3

I know this is a beginner question, but I can not understand how it works, on every source I find I see a different way to do the same, and I don't understand the difference between these ways to build a function in c which returns a string.

What is happening when:

I write the asterisk after the return type?

char* my_function(){...}

What if write the asterisk before function name?

char *my_function(){...}

What is the asterisk between both?

char * my_function(){...}

What 2 asterisk means?

char **my_function(){...}

And yes, the next code compiles:

char* * **my_function(){...}

OR... are they all the same thing?

stramin
  • 2,183
  • 3
  • 29
  • 58
  • 2
    Space is (mostly) irrelevant in C. `char*foo;`, `char *foo;`, `char* foo;`, and `char * foo;` all mean 100% absolutely the same. Some people have a preference for one or another, maybe your place of coding has a guideline about which to use; in short: use the one you like best unless there's a reason to use another style. – pmg Mar 08 '19 at 13:10
  • Possible duplicate of [C - initialization of pointers, asterisk position](https://stackoverflow.com/questions/4203009/c-initialization-of-pointers-asterisk-position) – Richard Chambers Mar 08 '19 at 13:10
  • Mostly a duplicate of "[What is the function of an asterisk before a function name?](/q/8911230/90527)"; if not a complete duplicate, then only because this asks more than one question. – outis Aug 08 '22 at 06:45

4 Answers4

9

char* my_function(){...} defines a function returning pointer to char.

char *my_function(){...} defines a function returning pointer to char. Same as above - different style.

char * my_function(){...} defines a function returning pointer to char. Same as above - different style.

What 2 asterisk means? --> a pointer to a pointer.

char **my_function(){...} defines a function returning pointer to pointer to char. Not the same as above - different return type.

char* * **my_function()(){...} defines a function returning pointer to pointer to pointer to pointer to char. Not the same as above - different return type.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
5

So long as the asterisks lie between char and my_function, the spacing doesn't make any difference.

In all cases they form part of the return type of the function.

char* means the return type is a pointer to a char.

char** means the return type is a pointer to a pointer to a char.

And so on.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
4

In a declaration, T *p, T* p, and T * p are all parsed as T (*p) - the * is part of the declarator, not the type specifier. So the first three function declarations all declare my_function to return a pointer to char.

T **p declares p as a pointer to a pointer to T. Yes, multiple indirection is possible, and you can have pointers to pointers, pointers to pointers to pointers, etc.

Basic rules:

T *p;        // p is a pointer to T
T *p[N];     // p is an array of pointer to T
T (*p)[N];   // p is a pointer to an array of T
T *f();      // f is a function returning a pointer to T
T (*f)();    // f is a pointer to a function returning T
T const *p;  // p points to a const T
const T *p;  // same as above
T * const p; // p is a const pointer to T
John Bode
  • 119,563
  • 19
  • 122
  • 198
2

The asterisk denotes pointers, they're one of the most important concepts to have a grasp on if you want to be programming in C.

char* is a pointer to a char, char** is a pointer to a char*.

The spacing is irrelevant.

I suggest you take a more in-depth look at pointers, what they are and how to use them.

GBrandt
  • 685
  • 4
  • 11
  • 2
    "Internally they're the same thing (a long pointer)" - ruins the answer. You don't know that. – Bathsheba Mar 08 '19 at 13:13
  • Aren't all pointers stored as 32/64-bit integers? Might have misused some words there, but I'm pretty sure pointer types are interchangeable (you could cast int* to char*, altough that might be a bit dumb and go very wrong) exactly because of that. – GBrandt Mar 08 '19 at 13:17
  • 2
    Not necessarily. That's common indeed, but the C standard doesn't specify that. On some systems `sizeof(char*)` could be different to `sizeof(double*)` for example. Note that the current crop of Intel chips use 48 bit pointers under the hood. – Bathsheba Mar 08 '19 at 13:17
  • Right, but all pointers (not the memory they point to) are the same size, right? Or is there some special situation where int* is 64-bit and char* is, let's say, 48-bit? **Edit:** Oh, ok, saw your edit. I wasn't aware of that, thanks for the info \o. – GBrandt Mar 08 '19 at 13:19
  • I'd still remove that sentence though. Folk would probably upvote. I'd reverse my downvote. – Bathsheba Mar 08 '19 at 13:28
  • "*all pointers [...] are the same size, right?*" not necessarily. This depends on the implementation. On the wide spread x86 or x64 they probaly are, yes. – alk Mar 08 '19 at 13:28
  • Thank you. Here it comes -> here they come! – Bathsheba Mar 08 '19 at 13:29
  • 1
    @GBrandt: depending on the platform, pointers may not even be scalar values, but something along the lines of a page number and offset. The only requirements are that `char *` and `void *` have the same representation, all `struct` type pointers have the same representation, all `union` pointers have the same representation, and that pointers to signed types have the same representation as pointers to the corresponding unsigned type. Yes, on x86 they’re all the same, but there are oddball architectures out there. – John Bode Mar 08 '19 at 14:18