The difference between int * i and int ** i

What is the difference between int* i and int** i ?

+6
c ++ c pointers
source share
12 answers

Pointer to an integer value

 int* i 


Pointer to a pointer to an integer value

 int** i 

(That is, in the second case, two differences will be required to access the integer value)

+23
source share

int * pi

pi is a pointer to an integer

int ** ppi

ppi is a pointer to a pointer to an integer.

EDIT :

You need to read a good book on signs. I recommend C Pointers from Kenneth Rick .

+8
source share
  • int* i : i is a pointer to an object of type int
  • int** i : i is a pointer to a pointer to an object of type int
  • int*** i : i is a pointer to a pointer to an object of type int
  • int**** i : i is a pointer to a pointer to a pointer to an object of type int
  • ...
+7
source share

I do not think this is specific to opencv.

int *i declares a pointer to int. Thus, i stores the memory address, and C expects the contents of this memory address to contain int.

int **i declares a pointer to ... a pointer. To int. Thus, i contains the address, and at this memory address C expects to see another pointer. Therefore, this second memory address must contain int.

Note that while you declare a pointer to an int, the actual int is not allocated. Therefore, you can say int *i = 23 , which says: "I have a variable, and I want it to point to memory address 23, which will contain int." But if you try to actually read or write to memory address 23, you will probably be segfault, since your program does not "own" this piece of RAM. *i = 100 will be segfault. (The solution is to use malloc (). Or you can point it to an existing variable, as in int j = 5; int *i = &j )

+6
source share

Imagine that you have several friends, one of them should give you something (a treasure ... :-) Say that John has a treasure

 int treasure = 10000; // in USD, EUR or even better, in SO rep points 

If you ask directly john

 int john = treasure; int you = john; 

If you cannot join John, but the gill knows how to contact him,

 int john = treasure; int *gill = &john; int you = *gill; 

If you can’t even join the gill game, but must contact the first jake, which can contact the gill

 int john = treasure; int *gill = &john; int **jake = &gill; int you = **jake; 

Etc ... Pointers are only indirect actions.

This was my last story for today before going to bed :-)

+3
source share

I deeply believe that a picture is worth a thousand words. Take the following example

 // Finds the first integer "I" in the sequence of N integers pointed to by "A" . // If an integer is found, the pointer pointed to by P is set to point to // that integer. void f(int N, int *A, int I, int **P) { for(int i = 0; i < N; i++) if(A[i] == I) { // Set the pointer pointed to by P to point to the ith integer. *P = &A[i]; return; } } 

So, in the above example, A points to the first integer in a sequence of N integers. And P points to a pointer that the caller will have a pointer to the found integer stored in.

 int Is[] = { 1, 2, 3 }; int *P; f(3, &Is[0], 2, &P); assert(*P == 2); 

&P used to pass the address of function P to the function. This address is of type int ** because it is the address of a pointer to int.

+3
source share

Say you are a teacher and should give notes to one of your students.

 int note; 

Well ... I meant the whole class

 int *class_note; /* class_note[0]: note for Adam; class_note[1]: note for Brian; ... */ 

Well ... do not forget that you have several classes

 int **classes_notes; /* classes_notes[0][2]: note for Charles in class 0; ... */ 

And you also teach at several institutions.

 int ***intitute_note; /* institute_note[1][1][1]: note for David in class 1 of institute 1 */ 

etc. etc.

+2
source share

Not a declaration. The syntax of the declaration does not allow () throughout the declaration. What are they doing there () ? If this should be part of the function declaration, include the entire function of declaring the declaration in your question, since in general the actual value of the declaration may depend on this. (Not that, though.)

As for the difference ... In the first there is one * , and in the second - two * . Does it help? Probably no. The first declares i as a pointer to an int . The second declares i as a pointer to int * . Does it help? Probably not enough either. Without a more specific question, it is difficult to give a more meaningful answer.

Provide more context, please. Or, if it's actually as specific as it can get, read your favorite C or C ++ book on pointers. Such broad, general questions are not what you ask on the net.

+1
source share

int * i - integer memory cell address int ** - integer memory cell address

+1
source share

note that

 int *i 

not fully interchangeable with

 int i[] 

This can be seen from the fact that the following will compile:

 int *i = new int[5]; 

until it is:

 int i[] = new int[5]; 

For the second, you must give it a list of constructors:

 int i[] = {5,2,1,6,3}; 

You will also get some validation using the [] form:

  int *i = new int[5]; int *j = &(i[1]); delete j; 

compiles the warning for free, but:

  int i[] = {0,1,2,3,4}; int j[] = {i[1]}; delete j; 

issue warnings:

warning C4156: deleting an array expression without using the array form 'delete'; matrix form warning C4154: delete array expression; conversion to supplied pointer

Both of these last two examples will crash the application, but the second version (using the type of declaration []) will give a warning that you are shooting in the leg.

(Win32 console project C ++, Visual Studio 2010)

0
source share

Text replacement is useful here, but beware of using it blindly, as it can be confusing (as in the extended example below).

 T var; // var has type T T* var; // var has type "pointer to T" 

This works no matter what T:

 int* var; // pointer to int char* var; // pointer to char double* var; // pointer to double // advanced (and not pure textual substitution): typedef int int3[3]; // confusing: int3 has type "array (of size 3) of ints" // also known as "int[3]" int3* var; // pointer to "array (of size 3) of ints" // aka "pointer to int[3]" int (*var)[3]; // same as above, note how the array type from the typedef // gets "unwrapped" around the declaration, using parens // because [] has higher precedence than * // ("int* var[3];" is an array (size 3) of pointers to int) 

This works when T itself is a pointer type:

 typedef int* T; // T is a synonym for "pointer to int" T* var; // pointer to T // which means pointer to pointer to int // same as: int** var; 
0
source share

int * i; // i is a pointer to an integer. It may contain the address of an integer variable.

int ** i; // i is a pointer to an integer. It may contain the address of an integer pointer variable.

0
source share

All Articles