简体   繁体   English

这个符号**在C语言中意味着什么

[英]What does this symbol ** mean in the C language

Hi I'm new to the C language, can someone explains what ** symbol mean. 嗨,我是C语言新手,有人可以解释**符号的含义吗。

typedef struct _TREENODE {
        struct _TREENODE *Left, *Right;
        TCHAR key[KEY_SIZE];
        LPTSTR pData;
    } TREENODE, *LPTNODE, **LPPTNODE;

If x is a pointer, *x dereferences it. 如果x是指针,则*x取消引用。 **x is the same as *(*x) , so **x dereferences a pointer to a pointer. **x*(*x) ,因此** x将指针取消引用到指针。 (eg, it is the thing that is pointed to by the thing that x opints to). (例如,x反对的事物指向的事物)。

** is a pointer to pointer, it is also used for dereferencing a pointer variable. **是指向指针的指针,也用于取消引用指针变量。

 eg: int a=10,*b,**c;

    b=&a;
    c=&b;
    printf("the a value is:%d\n",a);

    printf("the b value is:%d\n",*b);
    printf("the c value is:%d\n",**c);

just execute this code you will get the idea about pointer to pointer. 只需执行此代码,您将获得有关指针指向指针的想法。

There are two things to know about * in C: 关于C中的*有两件事要了解:

  1. It's an operation . 这是一项手术 Doing *x on a pointer dereferences that pointer . 对指针执行*x取消引用该指针 Doing **x on a pointer can dereference a pointer to a pointer , and so on. 对指针执行**x可以将指针解除对指针的引用,依此类推。

  2. It's a type . 这是一种类型 Declaring a type of int *x means that it's a pointer to an int type. 声明一个int *x类型意味着它是一个指向 int类型的指针 Declaring int **x means that it's a pointer to a pointer to an int type. 声明int **x意味着它是一个指向 int类型的指针

Example: 例:

int main() {
   int foo = 4;
   int *bar = &foo;  // declaring a pointer to int type *bar
   int **baz = &bar;  // declaring a pointer to a pointer to int type **baz
   printf("foo: %d, *bar: %d, **baz: %d\n", foo, *bar, **baz);  // derefencing the pointer *bar and **baz
   return 0;
}

In you want to change a variable, you pass it by pointer (to the variable ). 在你想改变一个变量,您可以通过指针传递(给变量 )。

And if you want to change a pointer, you also pass it by pointer (to the pointer ) which is a double pointer. 如果你想改变一个指针,你也指针( 指针 ),这是一个双指针传递。

In a declaration, ** means pointer to a pointer. 在声明中, **表示指向指针的指针。 When evaluating an expression, ** dereferences a pointer to a pointer. 计算表达式时, **将指针取消引用到指针。

int** p; // Declares p to be a pointer to a pointer.

And... 和...

**p = 10;  // Dereferences p and assigns 10 to a memory location.

One common use of pointers to pointers is to represent dynamic 2D arrays. 指针的一种常见用法是表示动态2D数组。 For example, if you want to create a matrix of M rows and N columns, you could do: 例如,如果要创建一个包含M行和N列的矩阵,则可以执行以下操作:

 int** matrix = malloc(M*sizeof(*matrix));
 int i = 0, j = 0;
 for ( i = 0; i < M; ++i )
    matrix[i] = malloc(N*sizeof(*matrix[0]));

Usage of the double pointer: 双指针的用法:

 for ( i = 0; i < M; ++i )
    for ( j = 0; j < N; ++j )
       matrix[i][j] = 0; // Assigns a value to the element
                         // at the i-th row and j-th column.

If you want to use string pointer dereferencing, you would use: 如果要使用字符串指针解引用,则可以使用:

 for ( i = 0; i < M; ++i )
    for ( j = 0; j < N; ++j )
       *(*(matrix+i)+j) = 0;

Memory allocated for the matrix has to be freed in two passes also. 为矩阵分配的内存也必须分两次释放。

 for ( i = 0; i < M; ++i )
    free(matrix[i]);
 free(matrix);

** means a pointer to a pointer. **表示指向指针的指针。

Most of the time I like to think of it as "pointer(s)" to "a memory area". 大多数时候,我喜欢将其视为“内存区域”的“指针”。 Which in fact may be a little redundant. 实际上这可能有点多余。

For example, suppose you have to dynamically store several words on memory, how would you do that? 例如,假设您必须在内存中动态存储几个单词,该怎么做? There are several ways to do this, but I'll provide an example that illustrate the use of ** . 有几种方法可以做到这一点,但我将提供一个示例来说明**的用法。

Now, suppose you want to store three words: hi , hello and goodbye 现在,假设您要存储三个词: hihello再见

hi , hello and goodbye are strings, they consume, 2, 5 and 7 bytes on memory respectively. hihello再见是字符串,它们分别消耗内存中的2、5和7个字节。 Well, in fact it's 3, 6 and 8 bytes because of the \\0 , but lets not get into many details. 好吧,实际上由于\\0 ,它是3、6和8个字节,但让我们不涉及许多细节。

But one thing is clear, we need three memory areas to hold these strings and also three pointers to reference these memory areas later. 但是很明显,我们需要三个存储区来保存这些字符串,并且还需要三个指针来稍后引用这些存储区。

Note that one can just declare three pointers that points to these memory areas, but, would you be willing to declare one thousand pointers to hold one thousand words? 请注意,您只能声明三个指向这些存储区的指针,但是,您愿意声明一千个指针来容纳一千个字吗? This is where ** kicks in. 这是**插入的地方。

Example: 例:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NUMBER_OF_WORDS 3

int
main(int argc, char **argv)
{
        int  i;
        char **words;

        /* pointers */
        words    = malloc(sizeof(char*)*NUMBER_OF_WORDS);

        /* memory areas*/
        words[0] = strdup("Hi");
        words[1] = strdup("Hello");
        words[2] = strdup("Goodbye");

        for(i=0; i < NUMBER_OF_WORDS; i++)
                printf("%d) %s\n", i, words[i]);

        for(i=0; i < NUMBER_OF_WORDS; i++)
                free(words[i]); /* memory area */
        free(words); /* pointers */

        return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM