【问题标题】:Understanding pointers - c [duplicate]理解指针-c [重复]
【发布时间】:2013-01-11 04:58:01
【问题描述】:

可能重复:
What are the barriers to understanding pointers and what can be done to overcome them?

我对c和指针真的不熟悉,我想了解这里发生了什么:

typedef struct {
        int q[QUEUESIZE+1];
        int first;
        int last;
        int count;
} queue;
init_queue(queue *q)
{
        q->first = 0;
        q->last = QUEUESIZE-1;
        q->count = 0;
}

这样认为是否正确:q->first = 0 意味着如果一个分配给 '0' 地址某个值 'val',那么 *(q->first) 将返回 'val' ?

【问题讨论】:

  • *(q->first) 甚至不是有效代码。你能更好地解释你的问题吗?
  • 是什么阻碍了您获得一本 C 教科书并花一些时间学习基础知识。我无法想象这不会在任何地方详细说明。

标签: c pointers


【解决方案1】:

没有。 q->first = 0 将 0 分配给队列的属性 first。 q 是一个指针,但 q->first 是一个 int。

【讨论】:

    【解决方案2】:

    q->first(*q).first的简写
    括号是必要的,因为 . 将在 dereference * 之前被评估,并且因为 q 是一个指针 q.first == NOT A VALID THING


    queue aQ;
    init_queue(&aQ);

    函数init_queue 采用指向queue 的指针而不是指向int 的指针。
    这个函数的作用是把initialize结构的所有字段在以后的时候都可以被其他函数使用。

    【讨论】:

    • 对不起,但我不明白的是,如果 a 取一个 'int' i,然后我将它传递给 init_queue(&i), (*i).first 将等同于设置'typedef struct' 并为 i.first 赋值,不?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多