本质上我希望 qPtr[0] 保存 sPtr[0]
struct myQueue{
struct sample* node;
int front;
int size;
int numElements;
};
struct sample{
int field1[5];
char field2[10];
}
int main(){
struct myQueue* qPtr = malloc(10 * sizeof(struct myQueue);
struct sample* samplePtr = malloc(10 * sizeof(struct sample); //assume this array has been initialized
enqueue(qPtr, samplePtr[0]); //this does not work
}
//returns 1 if enqueue was successful
int enqueue(struct myQueue* qPtr, struct sample* sPtr){
qPtr->node[(qPtr->front + qPtr->numElements) % qPtr->size] = sPtr; //code pertains to circular array implementation of queues
return 1;
}
我已经研究了大约 2 个小时,希望能对我在概念上做错了什么进行澄清。谢谢!
最佳答案
samplePtr[0]
给出对象本身,而不是指向对象的指针。尝试发送 &samplePtr[0]
或 samplePtr
本身。 enque
函数,第二个参数需要 struct example*
类型,而不是 struct example
。
关于c 编程 : need fresh eyes to look at this [demo code ! = 作业],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5281145/