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(...);
6
  • Mentally separate int * (pointer to int) from ptr = (int *) malloc(sizeof(*ptr)); and you have your answer. Commented Nov 22, 2019 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. Commented Nov 22, 2019 at 13:42
  • OT: Noho need to cast void-pointers in C. Commented Nov 22, 2019 at 13:42
  • 1
    In C you don't have to cast the result of malloc (and such a cast could actually lead to bugs). Commented Nov 22, 2019 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. Commented Nov 22, 2019 at 13:52

5 Answers 5

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).

Sign up to request clarification or add additional context in comments.

Comments

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));

7 Comments

I think this is why I prefer to initialize it as int* ptr rather than int *ptr. This isn't a universal view, though.
Thank you. So this would be wrong? int *ptr = malloc(sizeof(*ptr));
@JohnGo-Soco Indeed it isn't - particularly in instances like int* ptr, variable;
@davidev What makes you think int *ptr = malloc(sizeof(*ptr)) is wrong?
@Jabberwocky I thought I need to assign a value after =, because I used *. But now I understood that the * is part of the declaration.
|
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 .

Comments

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.

Comments

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.

2 Comments

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 A pointer is a value, it's just interpreted differently by the compiler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.