I need help with the folling C program, I’m working with void* and I keep getting the error for the casting issue from int to void* on Xcode.
How can I fix this issue and print out the info from my queue?
NOTE: I tried (intptr_t) here to cast it but it did not work either.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct queueNode queueNode;
typedef struct queue queue;
struct queueNode{
void* data_;
queueNode* next_;
};
queueNode* queueNode_create(void* data);
queueNode* queueNode_create_next(void* data, queueNode* next);
queueNode* queueNode_create(void* data){
return queueNode_create_next(data, NULL);
}
queueNode* queueNode_create_next(void* data, queueNode* next){
queueNode* p = (queueNode*)malloc(sizeof(queueNode));
p->data_ = data;
p->next_ = next;
return p;
}
void queueNode_print(queueNode* q){ printf(“Data: %p Next: %p”, q->data_, q->next_); }
struct queue{
queueNode* head_;
int numItems_;
};
/*
create
empty
check_empty
size
front
back
pop_front
push_back
delete
*/
queue* queue_create();
bool queue_empty(queue* q);
void queue_check_empty(queue* q);
int queue_size(queue* q);
void* queue_front(queue* q);
void* queue_back(queue* q);
void queue_pop_front(queue* q);
void queue_push_back(queue* q, void* data);
void queue_print(queue* q);
void queue_delete(queue* q);
queue* queue_create(){
queue* q = (queue*)malloc(sizeof(queue));
q->head_ = NULL;
q->numItems_ = 0;
return q;
}
bool queue_empty(queue* q){ return q->numItems_ == 0; }
void queue_check_empty(queue* q){ if(queue_empty(q)) { fprintf(stderr, “<empty>n”); } }
int queue_size(queue* q){ return q->numItems_; }
void* queue_front(queue* q){
queue_check_empty(q);
return q->head_;
}
void* queue_back(queue* q){
queue_check_empty(q);
queueNode* temp = q->head_;
while(temp->next_ != NULL){
temp = temp->next_;
}
return temp;
}
void queue_pop_front(queue* q){
queue_check_empty(q);
q->head_ = q->head_->next_;
–q->numItems_;
}
void queue_push_back(queue* q, void* data){
queueNode* node = queueNode_create(data);
if(queue_size(q) == 0){
q->head_ = node;
}
else{
queueNode* temp = q->head_;
while(temp->next_ != NULL){
temp = temp->next_;
}
temp->data_ = node;
}
++q->numItems_;
}
void queue_print(queue* q){
queue_check_empty(q);
queueNode* temp = q->head_;
while(temp->next_ != NULL){
queueNode_print(temp);
temp = temp->next_;
}
}
void queue_delete(queue* q){
free(q);
}
int main(int argc, const char * argv[]) {
queue* q = queue_create();
int i;
for(i=0; i<10; ++i){ queue_push_back(q, (void*)(intptr_t)(i*2)); }
queue_print(q);
queue_pop_front(q);
queue_print(q);
queue_delete(q);
return 0;
}
//END
Expert Answer
you need to include stdint.h.
Below code is working fine.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
typedef struct queueNode queueNode;
typedef struct queue queue;
struct queueNode{
void* data_;
queueNode* next_;
};
queueNode* queueNode_create(void* data);
queueNode* queueNode_create_next(void* data, queueNode* next);
queueNode* queueNode_create(void* data){
return queueNode_create_next(data, NULL);
}
queueNode* queueNode_create_next(void* data, queueNode* next){
queueNode* p = (queueNode*)malloc(sizeof(queueNode));
p->data_ = data;
p->next_ = next;
return p;
}
void queueNode_print(queueNode* q){ printf(“Data: %p Next: %p”, q->data_, q->next_); }
struct queue{
queueNode* head_;
int numItems_;
};
queue* queue_create();
bool queue_empty(queue* q);
void queue_check_empty(queue* q);
int queue_size(queue* q);
void* queue_front(queue* q);
void* queue_back(queue* q);
void queue_pop_front(queue* q);
void queue_push_back(queue* q, void* data);
void queue_print(queue* q);
void queue_delete(queue* q);
queue* queue_create(){
queue* q = (queue*)malloc(sizeof(queue));
q->head_ = NULL;
q->numItems_ = 0;
return q;
}
bool queue_empty(queue* q){ return q->numItems_ == 0; }
void queue_check_empty(queue* q){ if(queue_empty(q)) { fprintf(stderr, “<empty>n”); } }
int queue_size(queue* q){ return q->numItems_; }
void* queue_front(queue* q){
queue_check_empty(q);
return q->head_;
}
void* queue_back(queue* q){
queue_check_empty(q);
queueNode* temp = q->head_;
while(temp->next_ != NULL){
temp = temp->next_;
}
return temp;
}
void queue_pop_front(queue* q){
queue_check_empty(q);
q->head_ = q->head_->next_;
–q->numItems_;
}
void queue_push_back(queue* q, void* data){
queueNode* node = queueNode_create(data);
if(queue_size(q) == 0){
q->head_ = node;
}
else{
queueNode* temp = q->head_;
while(temp->next_ != NULL){
temp = temp->next_;
}
temp->data_ = node;
}
++q->numItems_;
}
void queue_print(queue* q){
queue_check_empty(q);
queueNode* temp = q->head_;
while(temp->next_ != NULL){
queueNode_print(temp);
temp = temp->next_;
}
}
void queue_delete(queue* q){
free(q);
}
int main(int argc, const char * argv[]) {
queue* q = queue_create();
int i;
for(i=0; i<10; ++i){ queue_push_back(q, (void*)(intptr_t)(i*2)); }
queue_print(q);
queue_pop_front(q);
queue_print(q);
queue_delete(q);
return 0;
}