我对c和指针很新。每当我瘦下来我都理解它时,会出现一个我不太懂的问题(我花了一些时间阅读c文档,但指针仍然不清楚):
typedef struct {
int q[QUEUESIZE+1];
int first;
int last;
int count;
} queue;
enqueue(queue *q, int x)
{
if (q->count >= QUEUESIZE)
printf("Warning: queue overflow enqueue x=%d\n",x);
else {
q->last = (q->last+1) % QUEUESIZE;
q->q[ q->last ] = x;
q->count = q->count + 1;
}
}
我希望我的问题不会太不透明,但有人可以解释在排队函数中使用指针吗?我认为排队的原则是分配一些精确的连续内存地址,但这肯定不是......
答案 0 :(得分:3)
enqueue
接受一个队列队列( queue 类型的队列)并在其中添加一个元素(由一个整数组成。
queue *q
是一个指针,因为可能是
enqueue
按值传递队列,如
enqueue(queue q, int x) { ...
意味着
q
参数的副本)q
时,会在q
函数内的enqueue
上进行修改。
最初提供的队列(myqueue)作为参数不会被修改例如
enqueue(queue q, int x) {
q.count++; // only the local q.count is changed, not myqueue.count
// ...
}
// ...
queue myqueue;
// ...
enqueue (myqueue, 3); // enqueue changes its local parameter, myqueue is not affected
此外,可以优化enqueue
函数实现...(请参阅下面的 wildplasser 答案,建议更好的队列实现)
答案 1 :(得分:1)
struct queue {
unsigned first;
unsigned count;
int q[QUEUESIZE];
};
int enqueue(struct queue *q, int x)
{
if (q->count >= QUEUESIZE) {
fprintf(stderr, "Warning: queue overflow enqueue x=%d\n", x);
return -1;
}
q->q[ (q->first+q->count++) % QUEUESIZE ] = x;
return 0; /* success */
}
几点:
sizeof q->q / sizeof q->q[0]
取代,后者更强大。