4

I have a question regarding pointer initialization in C.

I understand that *ptr will give the value of that pointer is pointing to.

ptr will give you the address.

Now I got following syntax:

int *ptr = (int *) malloc(sizeof(*ptr));

Why is *ptr being initialized with an address of the Heap and not a value? malloc() returns an address right?

Shouldn't it be:

int *ptr;
ptr = malloc(...);
alk
  • 69,737
  • 10
  • 105
  • 255
davidev
  • 7,694
  • 5
  • 21
  • 56
  • Mentally separate `int *` (pointer to `int`) from `ptr = (int *) malloc(sizeof(*ptr));` and you have your answer. – Federico klez Culloca Nov 22 '19 at 13:41
  • The "problem" is that `*` is context-sensitive. It means "pointer to" in one context, "pointer dereference" in another and "multiplication" as well. – Federico klez Culloca Nov 22 '19 at 13:42
  • OT: Noho need to cast `void`-pointers in C. – alk Nov 22 '19 at 13:42
  • 1
    In C you [don't have to cast the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) (and such a cast could actually lead to bugs). – Some programmer dude Nov 22 '19 at 13:44
  • 1
    That is a **declaration**, when declaring a variable read it in reverse order and you'll get the sense: `int *ptr`=> **ptr** is a variable that deferenced with `*` gives an `int`. – Frankie_C Nov 22 '19 at 13:52
  • @FedericoklezCulloca: The `*` in a declaration and in an expression is not actually different or context-sensitive. In both cases, it means a dereference. The key is to realize that a declaration provides a picture of how something will be used. `int *ptr` says “When I use `*ptr`, that is an `int`.” So the `*` is a dereference operator; it is saying “When I dereference `ptr`, that is an `int`”. The declarator is a model of an expression, so the operators in it model the same meanings as in expressions. – Eric Postpischil Nov 22 '19 at 14:37

5 Answers5

5

In that line, int * is the type.

int *ptr = (int *) malloc(sizeof(*ptr));

Is just this compressed into one line:

int *ptr;
ptr = (int *) malloc(sizeof(*ptr));
David Schwartz
  • 179,497
  • 17
  • 214
  • 278
5

With *ptr, * is acting as the dereferencing operator.

With int *ptr, * is acting as part of the type declaration for ptr.

So the two things are entirely different, even though * is used. (Multiplication and comment blocks are further uses of * in C).

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

Actually , this:

int *ptr = (int *) malloc(sizeof(*ptr));  

Is just short syntax for this:

int *ptr;
ptr = malloc(...);

The * is used for defining a type pointer and not to dereference the pointer .

sagi
  • 40,026
  • 6
  • 59
  • 84
2

Both snippets above do the same thing.

In the first case, the * before ptr is not the derefernece operator but is part of the definition of the type. So you actually are assigning a value to (initializing, actually) ptr, not *ptr.

dbush
  • 205,898
  • 23
  • 218
  • 273
2

The difference between

int *ptr = (int *) malloc(sizeof(*ptr));

and

int *ptr;
ptr = malloc(...);

is basically the same as the difference between

int i = 5;

and

int i;
i = 5;

The first variant defines and initializes a variable in one go. The second variant defines the variable but leave it uninitialized, and then assign a value to it.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Okay thank you. So when I declare a pointer it expects an address after = ? No value right? I was confused because I used * at the beginning, but this is just for the definition of the pointer right? – davidev Nov 22 '19 at 13:47
  • @davidev A pointer *is* a value, it's just interpreted differently by the compiler. – Some programmer dude Nov 22 '19 at 13:50