困惑于二维结构的指针 [英] Confused in pointers of two dimensional struct

查看:79
本文介绍了困惑于二维结构的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用malloc创建动态结构.我的结构看起来像这样:

I am creating a dynamic structure using malloc. My struct looks like this:

typedef struct
    {
        int v=;
        int t;
        struct a *prev;//points to previous struct in same row
        struct a *next;//points to next struct in same row
        //Is this correct notation?
    } a;
    a **b;

在初始化期间,我这样做:

During initialization, I do this:

for(i=0;i<d1;i++)
    {
        for(j=0;j<d2;j++)
        {
            if (j==0) //if first block, point to NULL
                b[i][j]->prev = NULL;
            else //else point to previous block.
                b[i][j]->prev = *(b[i] + j - sizeof(b[i][j]));
        }
    }

我收到错误无效类型参数.这是怎么了对于上一个,我试图首先找到当前块*(b[i]+j)的地址并获取前一个块的地址,减去每个块的大小,即sizeof(b[i][j])

I am getting error invalid type argument. What is wrong here? For prev, I am trying to first find address of the current block *(b[i]+j) and to get address of previous block, subtracting the size of each block i.e. sizeof(b[i][j])

推荐答案

我猜想您希望每行中的元素都被链接,但不希望与前一行相对.我的看法:

I'm guessing that you want elements within each row to be dlinked but not against previous rows. My take:

void
fill(void)
{
    int d1;
    int d2;
    a **b;
    a *x;
    int i;
    int j;

    d1 = 10;
    d2 = 20;

    b = calloc(d1 * d2,sizeof(a));

    x = &b[0][0];

    for (i=0;  i<d1;  i++) {
        x->prev = NULL;
        x->next = x + 1;
        for (x++, j=1;  j<d2;  j++, x++) {
            x->prev = x - 1;
            x->next = x + 1;
        }
        x[-1].next = NULL;
    }
}

更新:

将n加到指针时的指针算术.如果n为1,则表示 not 不加1,则表示要添加sizeof(* ptr).请考虑以下所有条件:

Pointer arithmetic when you add n to a pointer. If n is 1, you are not adding 1 to the address, you're adding sizeof(*ptr). Consider that all of the following are equivalent:

int *xp;
int n;

(A1) xp += n;
(A2) xp = (int *) (((unsigned long) xp) + (n * sizeof(int)))
(A3) xp = &xp[n];
(A4) xp = (int *) (((char *) xp) + (n * sizeof(int)))

凝视一段时间,直到您真正理解为什么如此.这是.添加到指针的值应完全等于相同类型数组的索引值.

Stare at this for a while until you truly understand why this is true. This is key. The value you add to a pointer should exactly equal the index value of an array of the same type.

您的代码有以下几种破损方式:
-您需要用b[i][j].prev替换b[i][j]->prev,因为b[i][j]已经完全取消了对指针[的引用,这是其他人指出的]
-当您执行*(b[i] + j - sizeof(b[i][j])时,我什至不确定您会得到什么.
-您需要&b[i][j-1](A3).您的代码[如果完全有效],可能会生成&b[i][j-18].
-这是因为您的混合指针算术和字节大小(例如,您正在对上面的(A1),(A2)和(A3)进行混搭-您可以使用一个或另一个,但不能同时使用全部/全部)时间)

Your code is broken in a few ways:
- You need to replace b[i][j]->prev with b[i][j].prev because b[i][j] already fully dereferences the pointer [this was pointed out by others]
- When you're doing *(b[i] + j - sizeof(b[i][j]), I'm not even sure what you'll get.
- You want &b[i][j-1] (A3). Your code [if it works at all], probably produces &b[i][j-18].
- This is because your mixing pointer arithmetic and byte sizes (e.g. you're doing a mashup of (A1), (A2), and (A3) above--you can use one or the other but not both/all at the same time)

我的代码只需要在开始时设置一个指针,因为它在内部循环中增加了1.
请注意,完成"j循环"后,x == &b[i][d2]也是&b[i+1][0].这可能有助于使其更加清晰.
另外,我在内部j循环之前处理j == 0的情况,然后在之后修复"j == d2-1"的情况.这样,如果不是在内部循环内,为什么它从1开始,而不是0
请注意,x[-1].next(案例A3)是(x - 1)->next(案例A1)

My code only needs a single pointer that is set at the beginning because it's incremented by 1 in the inner loop.
Note that after finishing the "j loop", x == &b[i][d2] which is also &b[i+1][0]. That might help make it clearer.
Also, I handle the j==0 case before the inner j loop and post fix the "j==d2-1" case afterwards. That way, no if's inside the inner loop and why it starts at 1, not 0
Note that x[-1].next (case A3) is (x - 1)->next (case A1)

在这里,对循环进行了稍微的重新编码,以使其更简单/更快.它假定一个附加变量:a *ep;

Here the loops are recoded slightly to be a bit simpler/faster. It assumes an addition variable: a *ep;

for (i=0;  i<d1;  i++) {
    x->prev = NULL;
    x->next = x + 1;
    ep = x + d2;
    for (x++;  x<ep;  x++) {
        x->prev = x - 1;
        x->next = x + 1;
    }
    x[-1].next = NULL;
}

我认为您已经足够使您的代码正常工作.在那之后,尝试理解您的代码与我的代码以及为什么两者都以它们的方式工作.这样做将使您有能力处理指针和指针算术,并且它将很快像通过普通的固定大小的数组进行索引一样容易.编程愉快...

I think you've got enough to get your code working. After that, try to understand your code vs mine and why both work the way they do. Doing this will give you a leg up on dealing with pointers and pointer arithmetic and it will soon be as easy as indexing through an ordinary fixed size array. Happy Programming ...

这篇关于困惑于二维结构的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆