0

I have a program that creates, inserts into, deletes from and destroys priority queue in C. Everything works fine, but I have a task to make it easier for user to create the queue.

int Error;
PrQueue* q1= create(&Error);
PrQueue* q2 = create(&Error);
PrQueue* q3 = create(&Error);

Here is how I create the priority queue.

PrQueue* create(int* Error)
{
PrQueue* Meta = (PrQueue*)malloc(sizeof(PrQueue));
if(Meta == NULL)
{
    *Error = 1;
    return NULL;
}
Meta->NrOfItems = 0;
Meta->Head = NULL;
*Error = 0;
return Meta;
}

That's the function

typedef struct Element
{
int Data;
int Priority;
struct Element *Next;
struct Element *Prev;
} Element;


typedef struct
{
int NrOfItems;
struct Element *Head;

} PrQueue;

And that's the structure. All of these code parts are in different files. The first one is main.c , second one is functions.c and the third one is a .h header file. So what I need is somehow make it easier for the user to create the priority queue in main, I should somehow get rid of the pointers symbol in main. I don't really know how it should look, but as I understand it shouldn't use * in the main. Any ideas what and how I should change?

Thanks!

developer1
  • 73
  • 1
  • 6
  • 1
    You're going to have to explain more. It's difficult to make **easier to use code** than `PrQueue *create( int *Error )`. Maybe you could simply change your code to omit the `int *Error` parameter and use `errno` to indicate the reason for the error since the call to `malloc()` will set `errno` on failure. Other than that? Your code is simple, direct, and easy-to-understand. IMO anyone who needs it to be "easier" probably has problems spelling C, and forget about coding in it. Because it's **impossible** not to return a pointer when you're dynamically creating an object with `malloc()`. – Andrew Henle Feb 23 '19 at 14:32
  • `but as I understand it shouldn't use * in the main` You could [typedef the pointer](https://stackoverflow.com/questions/7624019/confusion-with-typedef-and-pointers-in-c) away. I consider this to be bad practice, but if it's what your instructor wants... – Nick ODell Feb 23 '19 at 14:39
  • @martin You should post to the Code Review Stack Exchange since you are in fact asking people to review your code. – dandan78 Feb 23 '19 at 14:44
  • Yeah, that's what I thought. Believe me, I didn't really understand myself what's wrong with this, but the task was clear "remove the * stars from main, so that it should be easier for user". I don't know, maybe I should somehow change it to void function or something like that? The int *Error part is irrelevant, I can omit that, but it's not the point. – developer1 Feb 23 '19 at 14:47
  • @NickODell Maybee! How can I make that real? – developer1 Feb 23 '19 at 14:48

1 Answers1

1

My recommendation is to convert your create() function to an init() function; this will allow the user to create PrQueue objects inside other objects and/or on the stack, rather than only on the heap:

void init(PrQueue * Meta)
{   
   Meta->NrOfItems = 0;
   Meta->Head = NULL;
}

It also means there is no possibility of the function failing, so the user won't have to do any error-checking.

The user could use it like this:

int main(int argc, char ** argv)
{
   PrQueue q;
   init(&q);
   [...]
}
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234