C++ int * i 和 int** i 之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3794236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:45:36  来源:igfitidea点击:

Difference between the int * i and int** i

c++cpointers

提问by M2star

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

int* i和 和有int** i什么区别?

回答by RJFalconer

Pointer to an integer value

指向整数值的指针

int* i


Pointer to a pointer toan integer value


指向整数值的指针的指针

int** i

(Ie, in the second case you will require two dereferrences to access the integer's value)

(即,在第二种情况下,您将需要两次取消引用才能访问整数值)

回答by Klaim

  • int* i: iis a pointer to a object of type int
  • int** i: iis a pointer to a pointer to a object of type int
  • int*** i: iis a pointer to a pointer to a pointer to object of type int
  • int**** i: iis a pointer to a pointer to a pointer to a pointer to object of type int
  • ...
  • int* i:i是指向类型对象的指针int
  • int** i:i是指向类型对象的指针的指针int
  • int*** i:i是指向类型对象的指针的指针int
  • int**** i:i是指向类型对象指针的指针的指针int
  • ...

回答by Prasoon Saurav

int* pi

整数*圆周率

piis a pointer to an integer

pi是一个指向整数的指针

int **ppi

国际**ppi

ppiis a pointer to a pointer to an integer.

ppi是指向整数指针的指针。

EDIT:

编辑

You need to read a good book on pointers. I recommend Pointers on C by Kenneth Reek.

你需要阅读一本关于指针的好书。我建议由Kenneth臭佬在C指针,指向

回答by poundifdef

I don't think this is specific to opencv.

我不认为这特定于 opencv。

int *iis declaring a pointer to an int. So istores a memory address, and C is expecting the contents of that memory address to contain an int.

int *i正在声明一个指向 int 的指针。所以i存储一个内存地址,并且 C 期望该内存地址的内容包含一个 int。

int **iis declaring a pointer to... a pointer. To an int. So icontains an address, and at that memory address, C is expecting to see another pointer. That second memory address, then, is expected to hold an int.

int **i正在声明一个指向……一个指针的指针。到一个整数。所以i包含一个地址,在那个内存地址,C 期望看到另一个指针。那么,第二个内存地址应该保存一个 int。

Do note that, while you are declaring a pointer to an int, the actual int is not allocated. So it is valid to say int *i = 23, which is saying "I have a variable and I want it to point to memory address 23 which will contain an int." But if you tried to actually read or write to memory address 23, you would probably segfault, since your program doesn't "own" that chunk of RAM. *i = 100would segfault. (The solution is to use malloc(). Or you can make it point to an existing variable, as in int j = 5; int *i = &j)

请注意,当您声明一个指向 int 的指针时,并未分配实际的 int。所以说 是有效的int *i = 23,即“我有一个变量,我希望它指向包含 int 的内存地址 23”。但是,如果您尝试实际读取或写入内存地址 23,您可能会出现段错误,因为您的程序并不“拥有”那块 RAM。*i = 100会段错误。(解决方案是使用 malloc()。或者您可以使其指向现有变量,如int j = 5; int *i = &j

回答by pmg

Let's say you're a teacher and have to 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 ... don't forget 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 ...

等等等等...

回答by e2-e4

Imagine you have a few friends, one of them has to give you something (a treasure... :-) Say john has the 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 gill knows how to contact him,

如果你不能加入约翰,但吉尔知道如何联系他,

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

If you cannot even join gill, but have to contact first jake who can contact gill

如果你甚至不能加入gill,但必须联系第一个可以联系gill的jake

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

Etc... Pointers are only indirections.

等等...指针只是间接的。

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

这是我今天睡前的最后一个故事:-)

回答by Johannes Schaub - litb

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, Apoints to the first integer in the sequence of N integers. And Ppoints to a pointer that the caller will have the pointer to the found integer stored in.

所以在上面,A指向N个整数序列中的第一个整数。并P指向一个指针,调用者将在其中存储指向找到的整数的指针。

int Is[] = { 1, 2, 3 };

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

&Pis used to pass the address of Pto the function. This address has type int **, because it's the address of a pointer to int.

&P用于将 的地址传递P给函数。该地址具有 type int **,因为它是指向 int 的指针的地址。

回答by AnT

Neither is a declaration. Declaration syntax does not allow ()around the entire declaration. What are these ()doing there? If this is supposed to be a part of function declaration, include the whole function declaration thing in your question, since in general case the actual meaning of a declaration might depend on that. (Not in this one though.)

也不是声明。声明语法不允许()围绕整个声明。这些人()在那里做什么?如果这应该是函数声明的一部分,请在您的问题中包含整个函数声明内容,因为在一般情况下,声明的实际含义可能取决于此。(虽然不是在这个。)

As for the difference... There is one *in the first and there are two *s in the second. Does it help? Probably not. The first one declares ias a pointer to int. The second one declares ias a pointer to int *. Does this help? Probably not much either. Without a more specific question, it is hard to provide a more meaningful answer.

至于区别……*第一个有一个,*第二个有两个。它有帮助吗?可能不是。第一个声明i为指向int. 第二个声明i为指向int *. 这有帮助吗?估计也不多。如果没有更具体的问题,就很难提供更有意义的答案。

Provide more context, please. Or, if this is actually as specific as it can get, read your favorite C or C++ book about pointers. Such broad generic questions is not something you ask on the net.

请提供更多上下文。或者,如果这实际上已经很具体了,请阅读您最喜欢的有关指针的 C 或 C++ 书籍。如此广泛的通用问题不是您在网上提出的问题。

回答by ananswer

int* i is the address of a memory location of an integer
int** is the address of a memory location of an address of a memory location of an integer

int* i 是整数内存位置
的地址 int** 是整数内存位置地址的内存位置

回答by ananswer

Textual substitution is useful here, but beware of using it blindly as it can mislead you (as in the advanced example below).

文本替换在这里很有用,但请注意不要盲目使用它,因为它会误导您(如下面的高级示例所示)。

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

This works no matter what T is:

无论 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 is itself a pointer type:

这在 T 本身是指针类型时有效:

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;