0

I'm trying to code a circular list but I have to add more than one information to each element of the list.

My queue is defined as

typedef struct queue {
    int data[maxsize];
    int f,r;
}myQueue;
myQueue q

And I need each element to be a struct like

typedef struct info{
    int number1;
    int number2;
    int number3;
    int number4;
} Element;

Any idea on how can I do that?

edit: Here's my enqueue function as well

void enqueue(myQueue *q,int num)
{
    if(empty(q)==1){
      q->f=0;
      q->r=0;
     }
     else{
      q->r=(q->r+1)%maxsize;
      }
    q->data[q->r]=num;
}

dequeue error

error: incompatible types when assigning to type 'int' from type 'Element {aka struct info}'
     del_num=q->data[q->f];
paragon
  • 15
  • 5
  • Instead of `int data[maxsize];`, you should declare `Element` before the queue, and then use `Element data[maxsize];` in the type `myQueue`. – Jonathan Leffler Dec 03 '17 at 01:40
  • That's what I get if I change it `code teste.c:109:14: error: incompatible types when assigning to type 'Element {aka struct info}' from type 'int' q->data[q->r]=num; ` – paragon Dec 03 '17 at 01:49
  • Use back-ticks ```…`around code`…``` in comments, and try to avoid having to put back-ticks into comments because it is painful – Jonathan Leffler Dec 03 '17 at 01:50
  • You get that error in code you've not shown. And line 109 is fairly large for an MCVE ([MCVE]). However, you shouldn't be assigning individual integers to the queue; you should be copying a structure: `add(Element e, myQueue *qp) { …; int x = …; qp->data[x] = e;… }` or perhaps : `add(const Element *e, myQueue *qp) { …; int x = …; qp->data[x] = *e;… }`. – Jonathan Leffler Dec 03 '17 at 01:53
  • I have edited my first post and put the function in there. – paragon Dec 03 '17 at 01:55
  • You can't add a single integer to a queue that expects structures. You must pass a structure, or structure pointer, to the `enqueue` function. – Jonathan Leffler Dec 03 '17 at 01:56
  • Woo got it! That fixed the problem in my enqueue function, now i'm stuck trying to figure out what to do with my dequeue function. – paragon Dec 03 '17 at 02:02
  • Note that to make text show as code, format the code (without tabs) in the text box and make it look as you want it to appear, ignoring the preview box. Then select the code and use the **`{}`** button above the text box to indent it 4 spaces so it is shown as code. You need a blank line above the code to separate it from running text. Now check the preview to make sure it still looks like you expected. You did make sure there were no tabs around, didn't you? – Jonathan Leffler Dec 03 '17 at 02:02
  • As to the `dequeue()` function, it will work with an `Element` too. Either you'll return an `Element` or it'll take a pointer to `Element` and you'll assign the data from the queue to the structure pointed at. `Element dequeue(myQueue *qp) { Element *ep = &qp->data[…]; return *ep; }` or `void dequeue(myQueue *qp, Element *ep) { *ep = qp->data[…]; }` (but there'll be extra code to adjust the indexes in the queue. – Jonathan Leffler Dec 03 '17 at 02:05
  • got it. how do I pass the reference to the struct? does it need to look like this? `enqueue(&q->number1, num);` – paragon Dec 03 '17 at 02:08
  • No type info in the last comment. `Element e = { … }; myQueue q; …; enqueue(&e, &q);` or reverse the arguments as appropriate. – Jonathan Leffler Dec 03 '17 at 02:12
  • oh I need to make sure the new element have the same type, right? it needs to be `Element newelement` and then use the function to enqueue it. Thank you a lot! – paragon Dec 03 '17 at 02:14
  • Sounds like you’ve cottoned on to the right idea. Yes, make sure you pass the right type. Make sure you’ve got function prototypes in scope so the compiler checks and makes sure you’re doing it right. – Jonathan Leffler Dec 03 '17 at 02:16
  • I'm trying to create a pointer to a element like this `Element *num; num = (Element *)malloc(sizeof(Element)); ` but I'm getting `error: request for member 'number1' in something not a structure or union`when I'm trying to scan. Am I doing something wrong? – paragon Dec 03 '17 at 05:23
  • You need to use `num->number1` when `num` is a pointer (`Element *num`); you'd use `num.number1` if `num` was an `Element` and not an `Element *`. – Jonathan Leffler Dec 03 '17 at 05:25
  • Oh right, fixed right away. The last problem is, when I'm trying to print the number I put in the queue, its printing a random number. E.g., enqueue(1) and I get 6487552 is enqueued as the output. – paragon Dec 03 '17 at 05:31
  • Well, when I last looked, you couldn't pass a single number to a function that expects at least two arguments, one of which should be a pointer to a queue and the other of which is either an `Element` or an `Element *`. That you aren't getting compilation errors for the wrong number of arguments, and the wrong types, means you are writing bogus code. Code written in the present millennium (say, within the last 5 years) should never be written without full function prototypes in scope before you call the function, and your compiler options should be ensuring that. – Jonathan Leffler Dec 03 '17 at 05:33
  • If you use GCC, you should be using `gcc -std=c11 -O3 -g -Wall -Wextra -Werror -Wstrict-prototypes -Wmissing-prototypes` as a starting point. If it isn't clean with those options, it probably isn't safe to run (well, the `-Werror` means it won't compile if there are warnings — it converts the warnings to errors).\ – Jonathan Leffler Dec 03 '17 at 05:35
  • I expressed myself poorly, my enQueue function is `void enqueue(myQueue *q,Element *num);`, so I'm using `enqueue(&q,num);` and after checking what I need to check in my enQueue function, I have `q->data[q->r]=*num;`. – paragon Dec 03 '17 at 05:40
  • OK; that sounds better, but it is difficult to guess what's going on if you aren't explicit. When you create `Element *num = malloc(sizeof(*num));`, how did you set the elements of `num`? `*num = (Element){ 1, 2, 3, 4 };` would be one way; `num->number1 = 234; num->number2 = 345; num->number3 = 456; num->number4 = 987654;` would be another. If you pass that to `enqueue()`, what do you find when you print the values in the function? What do you find when you print `num` in the calling code? Printing data to make sure it contains what you think it contains is a basic debugging technique. – Jonathan Leffler Dec 03 '17 at 05:43
  • I'm setting num with a scan `scanf("%d",&num->number1);` and I have 2 fuctions to print, one inside the enqueue function, which is the one that isn't working `q->data[q->r]=*num; printf("\n%d is enqueued\n",q->data[q->r]);`, and another one which is `printf("%d %d %d\n",q->data[i].number1, q->data[i].number2, q->data[i].number3);`and is working fine. I'm trying to understand what's wrong with my enqueue print function. – paragon Dec 03 '17 at 05:46
  • Well, `q->data[q->r]` is an `Element`, not a number. You need `q->data[q->r].number1`, probably. And if your compiler isn't warning you about type mismatches even inside a `printf()` you may need to get a better compiler. – Jonathan Leffler Dec 03 '17 at 05:48
  • Oh, just found it, my print function inside the enqueue was`printf("\n%d is enqueued\n",q->data[q->r]);` but when I change to `printf("\n%d is enqueued\n",q->data[q->r].number);` it works just fine. – paragon Dec 03 '17 at 05:48
  • Yeah it works now, thank you once again. – paragon Dec 03 '17 at 05:49
  • I can't be bothered to turn my comments into a coherent answer. I recommend deleting this question. – Jonathan Leffler Dec 04 '17 at 05:54

0 Answers0