【问题标题】:pointers to pointers and pointer arrays指向指针和指针数组的指针
【发布时间】:2010-08-11 11:00:42
【问题描述】:

gcc 4.4.4 c89

我理解指针没问题。但是,我正在加紧指针数组和指向指针的指针。

我一直在搞乱这段代码 sn-p 并留下了我认为我理解的内容。

如果我的 cmets 的代码行正确,非常感谢您提供任何建议?

void increment_ptr()
{
    /* Static char array */
    char src[] = "rabbit";
    /* pointer to array of pointers to char's - create 6 pointers in this array */
    char *dest[sizeof(src)];
    size_t i = 0;

    /* pointer to a char */
    char* chr_ptr = NULL;
    /* pointer to pointer that points to a char */
    char** ptr_ptr = NULL;

    /* chr_ptr pointer now points to the memory location where 'rabbit' is stored. */
    chr_ptr = src;
    /* ptr_ptr points to the first memory address of the pointer array of where dest is stored */
    ptr_ptr = dest;

    /* Deference chr_ptr and keep going until nul is reached 'rabbit\0'  */
    while(*chr_ptr != '\0')
        /* deference ptr_ptr and assign the address of each letter to the momory location where
           ptr_ptr is currently pointing to. */
        *ptr_ptr++ = chr_ptr++;

    /* reset the ptr_ptr to point to the first memory location 'rabbit' */
    ptr_ptr = dest;

    /* Keep going until NULL is found - However, my program never finds it, ends in UB */
    while(ptr_ptr != NULL) {
        /* Dereference what the pointer to pointer is pointing at the memory lcoation */
        printf("[ %s ]\n", *ptr_ptr++);
    }
}

【问题讨论】:

    标签: c pointers


    【解决方案1】:

    每个部分下面的评论(我没有提到的部分是正确的):

    /* Static char array */
    char src[] = "rabbit";
    

    此数组不是静态的 - 它具有 auto 存储持续时间。

    /* pointer to array of pointers to char's - create 6 pointers in this array */
    char *dest[sizeof(src)];
    

    这是一个指向 char 的指针数组,而不是指向数组的指针。数组的长度是 7,因为 sizeof(src) 是 7(它包括 nul 字符串终止符)。

    /* chr_ptr pointer now points to the memory location where 'rabbit' is stored. */
    chr_ptr = src;
    

    更准确地说,它指向src中的第一个字符,即"rabbit"中的'r'

    /* ptr_ptr points to the first memory address of the pointer array of where dest is stored */
    ptr_ptr = dest;
    

    它指向dest数组中的第一个指针。

    /* Keep going until NULL is found - However, my program never finds it, ends in UB */
    while(ptr_ptr != NULL) {
    

    正确 - 因为您从未初始化 dest。您可以将dest 的声明更改为:

    char *dest[sizeof(src)] = { 0 };
    

    ...它会起作用的。

    【讨论】:

      【解决方案2】:

      我建议您阅读在线 C-FAQ 的第 6 节:6. Arrays and Pointers

      【讨论】:

        【解决方案3】:

        错误是当您将 dest 分配给 ptr_ptr 时,这实际上是一个未初始化的指向字符的指针数组,使用 while 循环遍历它会失败。 p>

        /* reset the ptr_ptr to point to the first memory location 'rabbit' */
        ptr_ptr = dest;
        
        /* Keep going until NULL is found - However, my program never finds it, ends in UB */
        while(ptr_ptr != NULL) {
            /* Dereference what the pointer to pointer is pointing at the memory lcoation */
            printf("[ %s ]\n", *ptr_ptr++);
        }
        

        【讨论】:

        • 他在前一个循环中设置了 dest 的前 6 个成员的值(但第七个仍未初始化)。
        • 你说得对,我错过了那部分。但他确实还是跳过了'\0'
        猜你喜欢
        • 1970-01-01
        • 2017-09-03
        • 2023-03-11
        • 2016-07-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多