0

I was studying the data structure in C programming. and I got a question of pointer. there is a simple initialization of pointer variable head.

int* head = NULL;

I had to look into the notion of pointer in C. and now I got it. but,

when I use that code. I can't understand how *head could be assigned by a value NULL. because as I know, variable head(not *head) should have an int value first. (which is an address for type integer).

But It doesn't have any value in that code.

So... my opinion is.....

int* head;
head = (some address here);
*head = NULL;

this code does make sense to me. Because There is a value for head which has to be an address of type integer.

but still don't know how first code can work.

the first code:

int* head = NULL;
Noder
  • 313
  • 3
  • 15

5 Answers5

4
int* head = NULL;

in a function is equivalent to:

int* head;
head = NULL;    // Assign pointer value, *not* dereference

It is not equivalent to this (which would obviously be problematic because what would head point to?)

int* head;
*head = NULL;  // Dereference
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • "*... not equivalent to this*", because in this latter context the `*` acts as the "de-referring"-operator. Which made `*head` evaluate to an `int`, an `int` where `head` was pointing to, which it doesn't because it points to `NULL`, which is not a valid memory. – alk Jun 16 '18 at 14:02
  • In function context, it's undefined behavior because `head` was never initialized. – Jonathon Reinhart Jun 16 '18 at 14:16
4

When you say

int *head;

the name of the variable you are defining is head, and its type is "pointer to int", or int *.

When you say

int *head = NULL;

you're declaring the same variable head, and you're also giving it an initial value of NULL.

But since the name of the variable is head, if you want to define it on one line and then give it a value on a second line, do it like this:

int *head;
head = NULL;

But, it's true, this can be confusing at first.

For ordinary variables, you can easily imagine saying

int i = 5;

but then later changing it to

int i;
i = 5;

So if you say

int *head = NULL;

it's natural to want to change it to

int *head;
*head = NULL;

But this is wrong.

A big part of the confusion is that the * character ends up doing two different things.

When you say

int *head;

what the * is doing is saying that you're declaring a pointer. You're saying, "I'm declaring a variable named head, and it's a pointer, and what it points to is int."

But when you say

*head = NULL;

what the * is doing is saying "Take the pointer head, and figure out what it points to, and set the pointed-to location to NULL."

So when you say *head = NULL, you are not setting head to NULL; you are setting the pointed-to location to NULL. And that's a very different thing.

Bottom line: What you want here is

head = NULL;
Steve Summit
  • 45,437
  • 7
  • 70
  • 103
1

int* head = NULL; just means that head is a null pointer. NULL is just a macro for 0, and we've decided that a pointer with a value of 0 is pointing to nothing.

Gabriel
  • 829
  • 5
  • 12
1

int* header = NULL; works fine until you try to access the value stored at the address NULL which will throw an error.

Pointers are addresses, you can declare an address without specifying if any value is present at that address.

I think what is misleading you is NULL. NULL is used for pointers, not for effective values.

Leo
  • 741
  • 6
  • 14
  • "*or addresses*" which addresses? – alk Jun 16 '18 at 14:06
  • "*is used for pointers [...], not for effective values.*" a pointer is a variable just as any other in C. So a pointer also has a "value", like any other variable. – alk Jun 16 '18 at 14:08
  • 1
    Ok maybe my explanation is a bit confusing. Pointers are used to store values, like any other variables, but those values are addresses in memory. – Leo Jun 16 '18 at 14:08
  • Ah well ok, still nitpicking: the pointer isn't the address, the pointer is a variable holding an address value. I'd consider the "address" itself being somewhere in the "hardware". – alk Jun 16 '18 at 14:10
  • "*maybe my explanation*" so why not adjust it? ;-) – alk Jun 16 '18 at 14:15
1
int* head = NULL;

Here you are defining a variable head of type int *. The assignment of NULL is to head itself, which means head is pointing to location 0. It is a common way of indicating that head has not yet got a valid value.

Using *head is a different thing altogether though the confusion is understandable.

alk
  • 69,737
  • 10
  • 105
  • 255
Steveh
  • 11
  • 2
  • "*... head has not yet got a valid value.*" well, `NULL` **is** a valid value for a pointer. Still is it not addressing valid memory. – alk Jun 16 '18 at 14:05
  • Also `NULL` not necessarily needs to refer to the physical address `0`. Although on many systems it does. Still, if converted to an `int` it *always* evaluates to `0`. – alk Jun 16 '18 at 14:13