如何初始化双指针

时间:2018-06-23 05:37:37

标签: c pointers

我正在尝试在ANSI C中使用双指针。

这是源代码:

#include <stdio.h>

int main(void) {


char ** str;

char ** str_zero = str;

*str++ = "Test";

*str = NULL;

while (*str_zero != NULL )
{
  printf("%s\n",*str_zero++);
}
}

我想知道为什么除非我先执行以下操作,否则该程序不起作用:

#include <stdio.h>

int main(void) {

static char * a = "The"; 

char ** str = &a;

char ** str_zero = str;

*str++ = "Test";

*str = NULL;

while (*str_zero != NULL )
{
  printf("%s\n",*str_zero++);
}
}

然后删除静态char变量。

奇怪的是,在删除静态字符* a之后,第一个源代码可以正常工作吗!!

编译器如何以这种方式工作?

2 个答案:

答案 0 :(得分:1)

  

我想知道为什么该程序不起作用

下面是您的代码,在其中添加了一些可以回答上述问题的注释。

#include <stdio.h>

int main(void) 
{
    char ** str;  // Now you have a "pointer to pointer to char" variable
                  // The value of the variable is uninitialized

    char ** str_zero = str; // Now you have another "pointer to pointer to char" variable
                            // But you initialize it using an uninitialized variable (str)
                            // That's undefined behavior so from now on anything may happen


    *str++ = "Test";  // Here you make "whatever str points to" point to a string literal
                      // and increment str
                      // As str is uninitialized this is again UB

    *str = NULL;          // UB as str is still uninitialized

    while (*str_zero != NULL ) // UB as str_zero was initialized from uninitialized str
    {
        printf("%s\n",*str_zero++);  // UB as str_zero was initialized from uninitialized str
    }
}

现在让我们看一下您的第二个代码示例:

#include <stdio.h>

int main(void) 
{
    static char * a = "The"; // Now you have a "pointer to char" variable
                             // It's properly initialized to point to a string literal

    char ** str = &a; // Now you have a "pointer to pointer to char" variable
                      // It's properly initialized to point to a char pointer

    char ** str_zero = str;  // Now you have another "pointer to pointer to char" variable
                             // It's properly initialized from another initialized variable

    *str++ = "Test";  // Here you make "whatever str points to" point to a string literal
                      // and increment str
                      // As str pointed to the variable a, the variable a now points
                      // to the string literal "Test"
                      //
                      // As str was incremented it does not point to the variable a anymore
                      // Now str points to a "char pointer" just after variable a

    *str = NULL;      // This is UB. str points to a "char pointer" just after variable a
                      // but you have not allocated a char pointer there so when
                      // you dereference str it's UB

    while (*str_zero != NULL )       // This is ok the first time but once str_zero is incremented
    {                                // it's again UB
        printf("%s\n",*str_zero++);  // This is ok the first time but once str_zero is incremented
    }                                // it's again UB

}

答案 1 :(得分:-2)

我认为以下是实现源代码的更好方法:

#include <stdio.h>

int main(void) 
{

static char * s = NULL;
char ** str = &s;
char ** str_zero = str;

*str++ = "Swiss";
*str = NULL;

printf("%s\n",*str_zero);
}