c 编程 : need fresh eyes to look at this [demo code ! = 作业]

标签 c pointers data-structures struct

本质上我希望 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/

相关文章:

c - 这两个实现之间有区别吗?

c - 如果作为 C 中函数的参数给出,是否复制数组

c - 折线 (¦) 作为 "The C Programming Language"书中的 OR 运算符

c - 指向字符串字符的指针数组

c - 为什么不同指针的大小不同

c++ - 如何在插入点后不重新索引/移动项目的情况下将数据插入到有序的、随机可访问的列表中?

c - C 中的字符串反转

c++ - 何时删除 try-catch block 中的指针

algorithm - 树 : Performance comparison between stack implementation and recursive call of Traversal in BST

c++ - 我的链表反转递归方法代码有什么问题?