首页 > 解决方案 > 为什么我的指针没有增加到下一个索引?

问题描述

所以,如果我将 buf[t_index] 更改为 *buf(在下面有一个之前和之后),并使用 buf++ 而不是 t_index++,我得到一个分段错误,这是什么原因?我的研究需要有限的功能线,我有 1 太多 =[。

前:

t_tetrimino     *ft_sort_list(char **buf, int x, int y, int block)
{
    t_tetrimino *curr;
    t_tetrimino *head;
    int         t_index;
    int         index;

    index = 0;
    t_index = 0;
    curr = (t_tetrimino*)malloc(sizeof(t_tetrimino));
    head = curr;
    while (buf[t_index] != NULL)
    {
        if (ft_validator(buf[t_index], 0, 0, 0) == -1)
            return (NULL);
        while (buf[t_index][index])
        {
            if (buf[t_index][index] == '#')
                (curr->x[block] = x) && (curr->y[block] = y) && block++;
            buf[t_index][index] == '\n' && index != 19 ? (y++ && (x = 0)) : x++;
            index++;
        }
        set_tetr_properties(&curr);
        reset_vars(&block, &x, &y, &index);
        t_index++;
    }
    return (head);
}

我想要的是:

t_tetrimino     *ft_sort_list(char **buf, int x, int y, int block)
{
    t_tetrimino *curr;
    t_tetrimino *head;
    int         t_index;
    int         index;

    index = 0;
    t_index = 0;
    curr = (t_tetrimino*)malloc(sizeof(t_tetrimino));
    head = curr;
    while (*buf != NULL)
    {
        if (ft_validator(*buf, 0, 0, 0) == -1)
            return (NULL);
        while (*buf[index])
        {
            if (*buf[index] == '#')
                (curr->x[block] = x) && (curr->y[block] = y) && block++;
            *buf[index] == '\n' && index != 19 ? (y++ && (x = 0)) : x++;
            index++;
        }
        set_tetr_properties(&curr);
        reset_vars(&block, &x, &y, &index);
        buf++;
    }
    return (head);
}

标签: cpointers

解决方案


推荐阅读