Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointers to pointers vs. normal pointers

Tags:

c

pointers

The purpose of a pointer is to save the address of a specific variable. Then the memory structure of following code should look like:

int a = 5; int *b = &a; 

...... memory address ...... value
a ... 0x000002 ................... 5
b ... 0x000010 ................... 0x000002

Okay, fine. Then assume that now I want to save the address of pointer *b. Then we generally define a double pointer, **c, as

int a = 5; int *b = &a; int **c = &b; 

Then the memory structure looks like:

...... memory address ...... value
a ... 0x000002 ................... 5
b ... 0x000010 ................... 0x000002
c ... 0x000020 ................... 0x000010

So **c refers the address of *b.

Now my question is, why does this type of code,

int a = 5; int *b = &a; int *c = &b; 

generate a warning?

If the purpose of pointer is just to save the memory address, I think there should be no hierarchy if the address we are going to save refers to a variable, a pointer, a double pointer, etc., so the below type of code should be valid.

int a = 5; int *b = &a; int *c = &b; int *d = &c; int *e = &d; int *f = &e; 
like image 607
user42298 Avatar asked Jun 28 '16 13:06

user42298


People also ask

What is the difference between pointers and smart pointers?

A Smart Pointer is a wrapper class over a pointer with an operator like * and -> overloaded. The objects of the smart pointer class look like normal pointers. But, unlike Normal Pointers it can deallocate and free destroyed object memory.

What are pointers & how it is different from normal variables?

A pointer variable (or pointer in short) is basically the same as the other variables, which can store a piece of data. Unlike normal variable which stores a value (such as an int, a double, a char), a pointer stores a memory address. Pointers must be declared before they can be used, just like a normal variable.

What is normal pointer in C?

To understand pointers, it helps to compare them to normal variables. A "normal variable" is a location in memory that can hold a value. For example, when you declare a variable i as an integer, four bytes of memory are set aside for it. In your program, you refer to that location in memory by the name i.

Is it possible to declare a pointer to a pointer?

A pointer to a pointer is a form of multiple indirection, or a chain of pointers. Normally, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value as shown below.


1 Answers

In

int a = 5; int *b = &a;    int *c = &b; 

You get a warning because &b is of type int **, and you try to initialize a variable of type int *. There's no implicit conversions between those two types, leading to the warning.

To take the longer example you want to work, if we try to dereference f the compiler will give us an int, not a pointer that we can further dereference.

Also note that on many systems int and int* are not the same size (e.g. a pointer may be 64 bits long and an int 32 bits long). If you dereference f and get an int, you lose half the value, and then you can't even cast it to a valid pointer.

like image 71
Some programmer dude Avatar answered Sep 19 '22 05:09

Some programmer dude