c - 在 C 语言中取消引用指向指针等的指针

标签 c pointers

我校的任务如下:

创建一个函数,它将作为参数 ******str 指向指向指向 char 的指针的指针的指针的指针并设置跟着小白兔!到char的指针。

void mx_deref_pointer(char ******str);

我是 C 的新手并且非常困惑,尽管我已经了解了关于指针的所有知识..(

我想出了以下代码:

#include <stdio.h>
#include <stddef.h>

void mx_deref_pointer(char ******str) {
    char *pstr1, **pstr2, ***pstr3, ****pstr4, *****pstr5;
    str = &pstr5;
    pstr5 = &pstr4;
    pstr4 = &pstr3;
    pstr3 = &pstr2;
    pstr2 = &pstr1;
    pstr1 = "Follow the white rabbit!";
    printf("%s", pstr1);
}

int main() {
    char ******pstr6 = NULL;
    mx_deref_pointer(pstr6);
}

它确实输出了 Follow the white rabbit,但我认为这是不正确的,因为注释掉大部分函数仍然会产生相同的结果。另外,我不知道如何将 NULL 以外的任何内容传递给 mx_deref_pointer()。一些和我一起学习的人提出了不同的 mx_deref_pointer:

void mx_deref_pointer(char ******str) {
    str [0] [0] [0] [0] [0] [0] = "Follow the white rabbit!";
}

它似乎有效,但是没有人能够向我解释它是如何工作的。如果有人可以为此提供一段合适的代码,并且更重要的是解释它的作用和方式,我将不胜感激!

谢谢。

最佳答案

为了符合函数的参数(指向 char 的 6x 指针),指向 char 的 5x 指针的地址可以是调用者提供的参数(或 6x 指针的值) char,你可以选择,只要他们指的是有效数据)。因此,您拥有的所有这些堆栈指针都属于调用者 (main),而不属于函数。简而言之,函数和 main 应该看起来像这样:

#include <stdio.h>

void mx_deref_pointer(char ******str)
{
    static char msg[] = "Follow the white rabbit!";
    *****str = msg;
}

int main() 
{
    char *ptr1 = NULL;
    char **ptr2 = &ptr1;
    char ***ptr3 = &ptr2;
    char ****ptr4 = &ptr3;
    char *****ptr5 = &ptr4;

    mx_deref_pointer(&ptr5);

    puts(ptr1);
    return 0;
}

输出

Follow the white rabbit!

请注意,为了避免无意的未定义行为,赋值保存了包含字符串的非常量缓冲区的基地址。从技术上讲,您的编译器必须允许这样做:

void mx_deref_pointer(char ******str)
{
    *****str = "Follow the white rabbit!";
}

但这样做是不好的做法,因为调用者可能期望一个可修改的字符串。字符串文字是不可写,将其存储在char * 中将允许代码写入该字符串,而编译器不会发出任何警告。所以最好在返回之前将文字复制到可写数组中。

关于数组语法取消引用链的工作原理,给定一些非空类型指针 p,这些是等价的:

p[n] == *(p+n)

因此它们是等价的:

p[0] == *(p+0)

但是 *(p+0) 只是 *p。因此:

void mx_deref_pointer(char ******str)
{
    static char msg[] = "Follow the white rabbit!";
    *****str = msg;
}

等同于:

void mx_deref_pointer(char ******str)
{
    static char msg[] = "Follow the white rabbit!";
    str[0][0][0][0][0] = msg;
}

关于c - 在 C 语言中取消引用指向指针等的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56486393/

相关文章:

c - 如何根据 "Endianness"以位级别存储数据?

c - pthread_cancel() 函数未能终止线程

c - Visual Studio 2010 中的函数信息

c - 为什么 sk_buff 在 h union 中有一个 ip 头指针(ipiphdr),在 nh union 中有一个常规 ip 头指针(iphdr)?

C - 将多个指针作为参数传递给 pthread_create

pointers - 戈兰 : Get the value from list element

c - 在 Lua 中加载 C 库 -llua 未找到

c++ - 优化从子位掩码生成父位掩码

c - 另一个C指针问题

c++ - 如果没有指针,获取工厂函数指针会怎样?