c - 我如何测试这个 C 函数

标签 c pointers

我有一个奇怪的问题要作为练习:

Write a function which take a pointer of a pointer of a pointer of a pointer of a pointer of a pointer of a pointer of a pointer of a pointer of an int as a parameter and assign a value to it.

我认为我写的功能是正确的(如果不正确请更正)但是我该如何测试呢?

void function(int *********anInt)
{
    *********anInt = 5;
}

我试过了:

int main(void) {
    int *********nbr = malloc(sizeof(int));
    function(nbr);
    printf("%d", *********nbr);
}

但是我得到了一个段错误,我刚刚了解了 mallocpointers 所以我还没有完全理解它。

最佳答案

当然,你可以测试它,虽然它看起来很奇怪。

#include <stdio.h>

void function(int *********anInt)
{
    *********anInt = 5;
}

int main()
{
    int n = 0;
    int *p1 = &n;
    int **p2 = &p1;
    int ***p3 = &p2;
    int ****p4 = &p3;
    int *****p5 = &p4;
    int ******p6 = &p5;
    int *******p7 = &p6;
    int ********p8 = &p7;
    function(&p8);
    printf("%d\n", n);
    return 0;
}

关于c - 我如何测试这个 C 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17727996/

相关文章:

c - long double 在使用 MinGW 编译时给出错误的结果

python - 使用 ctypes 将 2d numpy 数组传递给 c

c - 在 C 中扫描和打印字符时出现奇怪的代码交互

c++ - C语言中如何使用内存地址?它们是十六进制还是无符号整数?

c - 如何比较指针与ascii字符?

c - 保持对 JNIEnv 环境的全局引用

c++ - 为什么即使我们将指针分配给 NULL,指针指向的对象的大小也不为零?

c - 警告 : comparison between pointer and integer array

c - 数组中的最大数量

c - C 中的文件问题