指向指针的指针和指针数组 [英] pointers to pointers and pointer arrays

查看:103
本文介绍了指向指针的指针和指针数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gcc 4.4.4 c89

gcc 4.4.4 c89

我明白指针了.但是,我正在使用指针数组和指向指针的指针.

I understand pointers ok. However, I am stepping up to pointer arrays and pointers to pointers.

我一直在弄弄这段代码片段,并对我认为的理解发表了评论.

I have been messing around with this code snippet and have left comments of what I think I understand.

如果我的注释与代码行正确,非常感谢您的任何建议?

Many thanks for any advice if my comments are correct with the line of code?

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++);
    }
}

推荐答案

每个部分下面的注释(我没有提到的位是正确的):

Comments below each part (bits I haven't mentioned are correct):

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

此数组不是静态的-存储时间为auto.

This array is not static - it has auto storage duration.

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

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

This is an array of pointers to char, not a pointer to an array. The length of the array is 7, because the sizeof(src) is 7 (it includes the nul string terminator).

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

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

More precisely, it points at the first character in src, which is the 'r' in "rabbit".

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

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

It points at the first pointer in the dest array.

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

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

Correct - because you never initialised dest. You could change the declaration of dest to this:

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

...它将起作用.

这篇关于指向指针的指针和指针数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆