a***@gmail.com
2017-03-23 16:56:59 UTC
Hi all I am trying to make a sample queue program. I just started with this concept and I am seeing a problem
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct queue{
struct node *front;
struct node *rear;
};
struct queue *q=NULL;//pointer is initialised
q->front = NULL;//pointer members are initialised
q->rear = NULL;//initialised
int main(void)
{
struct node *ptr = NULL;
/*q->front = NULL;
q->rear = NULL;*/
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("error allocationg memory\n");
return 1;
}
ptr->next = NULL;
ptr->data = 10;
if(q->front == NULL)
{
q->front = ptr;
q->rear = ptr;
//rest of the logic
}
free(ptr);
return 0;
}
I am getting two problems,
1)If I create
q->front = NULL;
q->rear = NULL;
as global I get this warning
error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token
q->front = NULL;
^
queue.c:15:2: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token
q->rear = NULL;
2) If I create same variables inside main, I can compile, but at runtime I am getting segmentation fault.
Why is this happening?
my setup is **gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)**
maybe this is a stupid question.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct queue{
struct node *front;
struct node *rear;
};
struct queue *q=NULL;//pointer is initialised
q->front = NULL;//pointer members are initialised
q->rear = NULL;//initialised
int main(void)
{
struct node *ptr = NULL;
/*q->front = NULL;
q->rear = NULL;*/
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("error allocationg memory\n");
return 1;
}
ptr->next = NULL;
ptr->data = 10;
if(q->front == NULL)
{
q->front = ptr;
q->rear = ptr;
//rest of the logic
}
free(ptr);
return 0;
}
I am getting two problems,
1)If I create
q->front = NULL;
q->rear = NULL;
as global I get this warning
error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token
q->front = NULL;
^
queue.c:15:2: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token
q->rear = NULL;
2) If I create same variables inside main, I can compile, but at runtime I am getting segmentation fault.
Why is this happening?
my setup is **gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)**
maybe this is a stupid question.