What is the use of pointers?

Asked

Viewed 921 times

15

What is the use of pointer pointers, example:

int  var;
int  *p;
int  **pp;
var = 50;

I even understand the use of the simple pointer(*), but why use another pointer to reference this?

  • 1

    A common use and create linked lists, for example. From a read therein, and quite didactic.

  • Incredibly this was one of the guides I just opened...

2 answers

20


Let’s assume an 8 bit computer with 8 bit addresses and that has only 256 bytes of memory. This is part of the memory, the numbers above represent the addresses:

 54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
|    | 58 |    |    | 63 |    | 55 |    |    | h  | e  | l  | l  | o  | \0 |    |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+

We can note that at address 63 starts the string "hello". So in this case, if this is the only occurrence of "hello" in memory then,

const char *c = "hello";

... defines c as the read-only (read-only) string pointer "hello", and contains the value 63. c must be stored somewhere: in the example above is located at address 58. In addition to pointing to characters, but we can also point to pointers. EX:

const char **cp = &c;

Now cp points to c, That is, It contains the address of c (which is 58). Consider also:

const char ***cpp = &cp;

Now cpp stores the address of cp. So its value is 55 based on the example above, and it stores the address 60 itself.

Now the reason for having pointer hands:

  • The name of an array provides the address of its first element. So if the array contains elements of type’t', a reference to the array has the type’t *'. Now consider a t array: of course a reference to that 2d array will be of type '(t *)*' = ’t **', and is therefore a pointer pointer.

  • Even if an array of strings sounds like 1D. it’s actually 2D, since strings are arrays of characters. Consequently: 'char **'.

  • A 'f' function will have to accept a parameter of type’t **' if it is to change the variable of type’t *'.

Among other many utilities.

This information was translated from Stephan’s post: https://stackoverflow.com/questions/897366/how-do-pointer-to-pointers-work-in-c

  • 1

    Good answer. But it seems to me that there is a mistake in the phrase "And it stores the address 60 itself.". It gives the impression that the pointer cpp contains the value 60, but you didn’t mean that he’s allocated at the address 60?

9

One of the main uses of int **p is the construction of matrices. A pointer int *p can point to an array of integers (multiple integers at contiguous memory positions) and a "pointer to pointer" can point to an array of pointers, each pointing to an array of integers. Note that with this you can make an array (an array of arrays) with lines of different size.

Also, another common use is to pass the pointer as a reference to a function. This way you can pass a structure as a parameter to a function and it can change to where your pointer points internally (like pointing to another place because you had to use malloc internally to increase its size).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.