0

I have two functions in which one calls the other function and the title is the error I get. I am just wondering if there is a simple fix I am missing. Let me know if more information is needed. Thank you!

node createNode() {
newNode temp;//declare node
temp = (newNode)malloc(sizeof(struct node));//allocate memory
temp->next = NULL;//next point to null
return *temp;// return the new node
} 
void enqueue(queue* q, customer* data) {
// Create a new LL node
struct node* temp = createNode(data);//error line
LSNRQMNZ
  • 11
  • 1

1 Answers1

0

You want a return value of struct node*, so the return type should be struct node*.

Also naming the pointer to struct node as newNode looks very confusing (at least for me), so you shouldn't do that.

One more point is that casting results of malloc() family is considered as a bad practice.

Finally, you should check of malloc() succeeded.

struct node* createNode() { /* use proper return type */
    /* use non-confusing type */
    struct node* temp;//declare node
    temp = malloc(sizeof(struct node));//allocate memory
    if (temp == NULL) return temp; /* check if allocation succeeded */
    temp->next = NULL;//next point to null
    /* remove dereferencing */
    return temp;// return the new node
} 
void enqueue(queue* q, customer* data) {
    // Create a new LL node
    struct node* temp = createNode(data);//error line

Also it looks weird that the argument data is passed but ignored, but I won't fix this because I don't know how to fix.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Maybe using `calloc` instead of `malloc` would ensure the structure is fully initialized to zero. This is slightly slower but make sure everything has a known value and if something is forgotten later (like pointer initialization) it is likely produce a clear segment violation the a undefined behavior. – fpiette Jun 30 '21 at 14:49