c - 如何在 C 中使用 while 声明多个指针到指针?

标签 c loops pointers while-loop

我有这个代码:

#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/

相关文章:

c - 写入()调用失败 : No space left on device: ENOSPC handling

c - 第一次调用 strtok() 总是在我的 char* 数组中写入 0

c - 标准 C - 如何使用 1970 年 1 月 1 日以外的 unix 纪元时间戳?

loops - 在 Mathematica : multiple outputs 中循环

c++ - 在这种情况下我会使用哪个循环

c - 指针以及 a=1 和 *a =1 之间的区别

c - 在 C 中使用 pthread_join 时出现段错误

c - "ERROR interpreting JPEG image file( invalid JPEG file structure :SOS before SOF)"是什么意思?

java - 如何删除数组中的最小值?

c - char* 的合法使用