我有一个奇怪的问题要作为练习:
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);
}
但是我得到了一个段错误,我刚刚了解了 malloc
和 pointers
所以我还没有完全理解它。
最佳答案
当然,你可以测试它,虽然它看起来很奇怪。
#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/