Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct component_t{
    int details;
    int priority;
};

typedef struct component compo_t;

struct node
{
    compo_t *c;
    struct node* link;
};

typedef struct node node_t;

struct prio_queue
{
    node_t *head;
};

typedef struct prio_queue prio_t;
void init_queue(prio_t* p_queue);
void enqueue(prio_t* p_q, compo_t* c);
void dequeue(prio_t* p_q);
void disp(const prio_t* p_q);

void init_queue(prio_t *p_queue)
{
    p_queue->head=NULL;
}

void enqueue(prio_t* p_q, compo_t* con){
    node_t temp = (node_t)malloc(sizeof(node_t));
}

void dequeue(prio_t* p_q){
    if(p_q->head==NULL){
        printf("No elements\n");
    } else{
        node_t *present=p_q->head;
        node_t *prev=NULL;
        int max = 0;
        node_t *prev_max = NULL;

        while(present != NULL){
            if(present->c.priority->max){
                max=present->c.priority;
                prev_max=prev;
            }
            prev = present;
            present=present ->link;
        }
        compo_t compo;
        if(prev_max != NULL){
            node_t *temp = prev_max -> link;
            prev_max -> link=temp -> link;
            compo.details = temp -> c.details;
            free(temp);
        }
        else{
            node_t *temp = p_q -> head;
            p_q -> head = p_q -> head -> link;
            compo.details=temp ->c.details;
            compo.priority =temp ->c.priority;
            free(temp);
        }
    }

}

void disp(const prio_t* p_q){
    node_t *p = p_q -> head;

    if(p==NULL){
        printf(" ");
    } else{
        while(p!=NULL){
            printf("%d %d\n", p->c.details, p ->c.priority);
            p=p -> link;
        }
    }
}

int main(){

    prio_t queue;
    init_queue(&queue);
    compo_t c;
    int choice;
    scanf("%d\n", &choice);
    do{
        switch(choice){
            case 1:
            scanf("%d %d\n", &c.details,&c.priority);
            enqueue(&queue,&c);
            break;

            case 2:
            dequeue(&queue);
            break;

            case 3:
            disp(&queue);
            break;

            default:
            exit(0);

        }
    } while(choice<4);
    return 0;

}



I am getting an error in the line: 
compo_t *c; in the struct node
.How do I resolve this error?


The error it says is "
error: conversion to non-scalar type requested
"
>

What I have tried:

I tried making 'c' as a pointer by giving *c, but in vain
Posted
Updated 5-Jun-22 1:58am
v2

You do not use component_t, node and other struct names.
Better directly use typedefs to reduce sources of error.
C
typedef struct  {
	int details;
	int priority;
} compo_t;

typedef struct {
	compo_t *c;
	node_t *link;
} node_t;

typedef struct {
	node_t *head;
} prio_t;

The malloc function always returns a pointer.
C
void enqueue(prio_t* p_q, compo_t* con) {
	node_t *temp = (node_t*)malloc(sizeof(node_t));
	// ...
}

It would also be better if all functions where something could go wrong had a return value.
 
Share this answer
 
v2
Comments
Achyuth S.S. 5-Jun-22 8:23am    
Thank you, I got the error resolved!!
merano99 5-Jun-22 12:56pm    
If my post helped, it would be nice if it were marked as a solution.

The problem is that component is not defined - component_t is, but component isn't. So the compiler doesn't know what size a compo_t is and it all starts to fall apart.
Try this:
C
typedef struct component_t compo_t;
And see if that error (I'm sure there are others in there!) goes away.
 
Share this answer
 
Comments
Achyuth S.S. 5-Jun-22 5:58am    
I did so, well, that error vanished. But in line - "compo.details = temp -> c.details;", gave an error stating - error: assignment to expression with array type
OriginalGriff 5-Jun-22 6:43am    
As I said, there are other problems - not the least is that that doesn't look like code that you sat down and thought about before you dived into code ... which is probably why it's throwing up other errors, and will continue to do so as you fix them one by one.

You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!
Achyuth S.S. 5-Jun-22 8:24am    
Yeah, I do agree with u, and my error is resolved thanks
OriginalGriff 5-Jun-22 8:32am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900