3

I think that *something and * something are different.

What does the additional white space do?

occurs here -> void * malloc( size_t number_bytes );

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
Delirium tremens
  • 4,623
  • 9
  • 46
  • 59
  • 3
    An asterisk with a space...? You need to be more specific. Where is this occurring? What are you doing in your code? – BoltClock Aug 10 '10 at 19:28
  • 2
    What!? Are you talking about difference between `char* someWord` and `char *someWord` (in which case, there is none)? – Justin Niessner Aug 10 '10 at 19:28
  • 11
    It is an indication that you should probably consult [a good introductory book on C](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – James McNellis Aug 10 '10 at 19:31
  • 1
    This is a newbie question, but to be fair, the "declaration reflects use" convention in C for pointers and dereference operators is quite confusing when you first learn it. I don't think the down votes were justified. – A. Levy Aug 10 '10 at 19:51
  • possible duplicate of [In C, why is the asterisk before the variable name, rather than after the type?](http://stackoverflow.com/questions/398395/), [What's your preferred pointer declaration style, and why?](http://stackoverflow.com/questions/377164/) – outis Dec 20 '11 at 22:41

7 Answers7

13
int* foo == int *foo == int * foo == int  *   foo

The whitespace does not make any difference to the compiler.

Justin Ardini
  • 9,768
  • 2
  • 39
  • 46
10

When you use an asterisk to get the value of an address it is called the dereference operator. For example:

int x = *something;

In the example in your question the asterisk has a different meaning because it is part of a type, not part of an expression. It is used to specify that the return type is a pointer (in your specific example, a void pointer).

The extra space does not mean anything and is ignored by the compiler. It is there only to aid readability.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
2

The * operator in a declaration always binds to the declarator; the line

void * malloc (size_t number_bytes);

is parsed as though it had been written

void (*malloc(size_t number_bytes));

It's an accident of C syntax that you can write T *p; or T* p; or even

T             *                     p; 

but all of them are parsed as T (*p); -- the whitespace makes no difference in how the declarations are interpreted.

John Bode
  • 119,563
  • 19
  • 122
  • 198
1

C ignores extraneous whitespace, so "* " should have the same effect as "*".

If you want more clarification, please post a code example.

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
0

The same thing, but with some whitespace added.

Mike Caron
  • 14,351
  • 4
  • 49
  • 77
0

Check out this article on Pointers

Those two lines do the exact same thing.

Robert Greiner
  • 29,049
  • 9
  • 65
  • 85
0

In your void * malloc(...) example, void * is the type. malloc returns a pointer to void, which is just a pointer that needs to be cast to a particular type in order to be useful.

nmichaels
  • 49,466
  • 12
  • 107
  • 135