首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >指向指针和指针数组的指针

指向指针和指针数组的指针
EN

Stack Overflow用户
提问于 2010-08-11 19:00:43
回答 3查看 951关注 0票数 2

gcc 4.4.4 c89

我能理解指针的意思。但是,我将逐步介绍指针数组和指针。

我一直在摆弄这个代码片段,并留下了我认为我理解的评论。

非常感谢任何建议,如果我的意见是正确的代码行?

代码语言:javascript
复制
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++);
    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-08-11 19:30:01

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

代码语言:javascript
复制
/* Static char array */
char src[] = "rabbit";

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

代码语言:javascript
复制
/* pointer to array of pointers to char's - create 6 pointers in this array */
char *dest[sizeof(src)];

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

代码语言:javascript
复制
/* chr_ptr pointer now points to the memory location where 'rabbit' is stored. */
chr_ptr = src;

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

代码语言:javascript
复制
/* ptr_ptr points to the first memory address of the pointer array of where dest is stored */
ptr_ptr = dest;

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

代码语言:javascript
复制
/* Keep going until NULL is found - However, my program never finds it, ends in UB */
while(ptr_ptr != NULL) {

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

代码语言:javascript
复制
char *dest[sizeof(src)] = { 0 };

...and它会起作用的。

票数 7
EN

Stack Overflow用户

发布于 2010-08-11 19:39:32

我建议您阅读在线C-常见问题解答的第6部分:6. Arrays and Pointers

票数 4
EN

Stack Overflow用户

发布于 2010-08-11 19:31:01

错误是当您将目标赋值给ptr_ptr时,这实际上是一个指向字符的未初始化指针数组,使用while循环遍历它将失败。

代码语言:javascript
复制
/* 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++);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3457578

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档