0

In C I try to learn queue data structure and make pointer to struct but inside the struct there is pointer to array. Here queue is struct, q is pointer to the struct, and inside struct there are rear, front, num and int *que(pointer to array to store data)

typedef struct {
    int max;
    int rear;
    int front;
    int num;
    int *que;
} queue;

And malloc() using

queue->que=malloc(sizeof(int)12) ; to make arrray And to access it, 
q->que[q->rear++] //not to familiar,

First I'm not declaring array but can I access data pointed by que using []? Is this mean access que[q->rear++] inside q pointer? Is this the same as (q).que[q->rear++]? I got segmentation fault .

Part of the code; but there are some errors

#include <stdio.h>
#include <stdlib.h>
typedef struct {
    int max;
    int num;
    int front;
    int rear;
    int *que;
} queue;


int initialization(queue*q, int max) {
    q->num = 0;
    q->front = 0;
    q->rear = 0;
    if (q->que =(int*)malloc(max * sizeof(int)) == NULL) { // got pointer NULL  i dont know why
        q->max = 0;
        return-1;
    }
    q->max=max;
    return 0;
}


int main() {
    queue que;
    if (initialization(&que, 12) == -1)
        printf("fail");
    else {
        int m,x;
        while (m != 0) {
            printf("1.enque 2.deque. 3.peek 4.display 0. slese");
            scanf("%d", &m);
            switch (m) {
                case 0: break;
                case 1: printf("data");
                    scanf("%d", &x);
                    enqueue(&que, x);
                    break;
                case 2:  dequeue(&que, &x);
                    printf("%d is dequeue", x);
                    break;
                case 3:x=peek(&que,&x);
                    printf("max now is %d", x);
                    break;
                case 4:display(&que);
        }
    }
}

int enqueue(queue*q, int x) {
    if (q->num >= q->max)
        return -1;
    else{
        q->num++;
        q->que[q->rear++]= x; //got segmentation fault
        if (q->rear == q->max)
            q->rear = 0;
    }
}
J...S
  • 5,079
  • 1
  • 20
  • 35
fiksx
  • 176
  • 1
  • 15
  • Put `#include ` – BLUEPIXY Oct 17 '17 at 03:43
  • 2
    First, please format your code with regular indentation; without it your code is *very* difficult to read. Second, `if (q->que =(int*)malloc(max * sizeof(int)) == NULL)` doesn't do what you want it to do because of operator precedence; this is effectively `q = (malloc() == NULL)`, so `q` ends up as 0 or 1. You want `if ((q = malloc()) == NULL)` instead. Oh, and please don't ignore compiler warnings - `m` is not initialised before use, for instance. Fix all these things then see how the program works. – Ken Y-N Oct 17 '17 at 03:43
  • @Ken Y-N thanks it works! instead of `int *que` and malloc to make array, can i just declare array such as `int que [12]` and access data with `q->que[ ]`? and also if i write` *(int)malloc (sizeof(int*12)` do i need cast too? and for operator precedence -> and [ ] is the same and it will be from left to right order. `q->que` will be evaluated first then [ ], am i right? – fiksx Oct 17 '17 at 07:21
  • Yes, you could use `int que[12];`, and in C you should *not* cast `malloc()`. If precedence is equal, [refer to this table for l-to-r or r-to-l association](http://en.cppreference.com/w/c/language/operator_precedence) - l-to-r in this case. – Ken Y-N Oct 17 '17 at 07:34
  • @KenY-N ok i got it, thanks for the help!! – fiksx Oct 18 '17 at 14:56

1 Answers1

0

In your initialization() function, while allocation memory with malloc() like

if (q->que =(int*)malloc(max * sizeof(int)) == NULL) {

the (int*)malloc(max * sizeof(int)) part is evaluated first and then this value is compared against NULL via the == operator. This will result in 0 if the condition is false and 1 otherwise.

Now this (0 or 1) value is being assigned to q->que instead of the return value of malloc(). So bottom-line is that q->que points to the memory location 0 (or 1 as the case maybe) which is most probably not a part of memory that a normal program is allowed to mess with and hence you get error.

You can solve this operator precedence problem by using parenthesis like

if ((q->que = malloc(max * sizeof(int))) == NULL) {

And in your while loop inside main(), the control expression is m!=0 but m is not even initialised during the first iteration. At that point, its value is indeterminate (garbage value).

You could initialise m to something other than 0 first like

int m=1,x;
while (m != 0) {

And in C, you needn't cast the value returned by malloc(). See here.

J...S
  • 5,079
  • 1
  • 20
  • 35
  • Thankyou so much!! I have one more question, a[c++ %max] i found this, if the operator precedence ++ is higher than % why it execute % first then ++? – fiksx Oct 18 '17 at 13:32
  • @fiksx `++` itself is done before `%` conforming to the precedence rule. However, it is postfix `++`. So the operation is done only after taking the value from it. If you use prefix `++`, you can tell the difference. Like ` a[++c %max]`. – J...S Oct 18 '17 at 13:55
  • 1
    Okk thankyou so much!! Yeah if it is ++c it will increment first then execute %. Thanks for the help! – fiksx Oct 18 '17 at 14:56