我有这个代码:
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
void ft_ultimate_ft(int *********nbr)
{
printf("%d", *********nbr);
}
int main(){
/*Start of problem*/
int *a;
int **b = &a;
int ***c = &b;
int ****d = &c;
int *****e = &d;
int ******f = &e;
int *******g = &f;
int ********h = &g;
int *********i = &h;
/*end of problem*/
*********i = 42;
ft_ultimate_ft(i);
return 0;
}
我需要在循环中包含指针到指针声明(例如 while)。需要减少声明数量。
最佳答案
我假设我已经正确理解了你的问题,即使用循环创建一个指向数字的多指针,然后为其赋值。
我编写了一段代码,部分完成了您的要求,但仍然需要知道循环后有多少层。
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
void ft_ultimate_ft(int *********nbr)
{
printf("%d", *********nbr);
}
int main() {
int t = 10;
int n = 8;
void * p = &t;
for (int i = 0; i < n; i++)
{
void* * s = (void **)malloc(sizeof(void*));
*s = p;
p = s;
}
/*end of problem*/
int *********real = (int *********)p;
*********real = 42;
ft_ultimate_ft(real);
return 0;
}
输出42
程序的清理部分没有写
PS。在您的代码中,指针 a 是不确定的,我认为您的原始代码无法正常工作。关于c - 如何在 C 中使用 while 声明多个指针到指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45860999/