Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to understand the notations : * and ** with pointers

I have a problem with the pointers. I know what this does:

*name

I understand that this is a pointer.

I've been searching but I do neither understand what this one does nor I've found helpful information

**name

The context is int **name, not multiplication

Could someone help me?

like image 521
MLMH Avatar asked Apr 17 '15 07:04

MLMH


People also ask

Why do people struggle with pointers?

There are two types of people who struggle with the concept of pointers: People who are new to programming, People who have used languages that don't permit the programmer to directly reference memory locations (essentially most languages besides C/C++ or Pascal)

Are pointers difficult to understand?

Pointers are arguably the most difficult feature of C to understand. But, they are one of the features which make C an excellent language. In this article, we will go from the very basics of pointers to their usage with arrays, functions, and structure.

What does * do to a pointer?

Note: The notation can be a little confusing. If you see the * in a declaration statement, with a type in front of the *, a pointer is being declared for the first time. AFTER that, when you see the * on the pointer name, you are dereferencing the pointer to get to the target.

What does * and &indicate in pointer?

The * operator turns a value of type pointer to T into a variable of type T . The & operator turns a variable of type T into a value of type pointer to T .


1 Answers

NOTE: Without the proper context, the usage of *name and **name is ambiguous. it may portrait (a). dereference operator (b) multiplication operator

Considering you're talking about a scenario like

  • char * name;
  • char **name;

in the code,

  • *name

name is a pointer to a char.

  • **name

name is a pointer, to the pointer to a char.

Please don't get confused with "double-pointer", which is sometimes used to denote pointer to a pointer but actually supposed to mean a pointer to a double data type variable.

A visual below

enter image description here

As above, we can say

char value = `c`;
char *p2 = &value;   // &value is 8000, so p2 == 8000, &p2 == 5000
char **p1 = &p2;     // &p2 == 5000, p1 == 5000

So, p1 here, is a pointer-to-pointer. Hope this make things clear now.

like image 93
Sourav Ghosh Avatar answered Oct 08 '22 17:10

Sourav Ghosh