我的C程序没有编译或给出分段错误 [英] My C program does not compile or gives segmentation fault
问题描述
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.
我尝试过的事情:
我甚至无法理解变量为NULL,它给我编译错误。
如何理解这个问题并解决它?
What I have tried:
I am not able to understand that even I initialise variables to NULL, its giving me compilation errors.
How to understand this problem and solve it?
推荐答案
我的C程序没有编译或给出分段错误
My C program does not compile or gives segmentation fault
你需要选择,程序给出编译错误或给出分段错误,但不是两者。
你需要学习
- 动态内存分配(alloc或malloc)
- 指针
- 使用 - >
You need to choose, the program gives compile error or gives segmentation fault, but not both.
You need to study
- Dynamic memory allocation (alloc or malloc)
- pointers
- Usage of ->
struct queue *q=NULL;//pointer is initialised
不,它没有初始化,也无法为成员分配值。
以下是该语言作者对C语言参考书的链接。
C编程语言 - 维基百科,免费的百科全书 [ ^ ]
https://hassanolity.files.wordpress.com/2013/ 11 / the_c_programming_language_2.pdf [ ^ ]
http:// www。 ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf [ ^ ]
No, it is not initialized, and you can't assign values to members.
Here is links to references books on C by the authors of the language.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]
1)
q->front = NULL;//pointer members are initialised
q->rear = NULL;//initialised
这些是指令。说明必须放在功能中。使用此类函数外,会引发错误。唯一的例外是在声明时初始化变量,如
These are instructions. Instructions must be placed within functions. When using such outside of functions, an error is thrown. The only exception is the initialisation of a variable upon declaration as in
struct queue *q=NULL;
2)
您的 q
指针初始化为 NULL
但它没有指向有效的 struct queue
对象。所以
2)
Your q
pointer is initialised with NULL
but it is not pointing to a valid struct queue
object. So
q->front = NULL;
会尝试将零值写入地址零,这会导致分段错误。 />
你必须先创建一个 struct queue
对象并让 q
指向:
will try to write the value zero to the address zero which results in a segmentation fault.
You have to create a struct queue
object first and let q
point to that:
/* Base structure initialised with NULL pointers */
struct queue base = { NULL, NULL };
/* Pointer to the base structure */
struct queue *q = &base;
上面解决了两个问题:
它声明了两个全局变量并初始化它们但不是使用任何说明。
The above solves boths problems:
It declares two global variables and initialises them but does not use any instructions.
这篇关于我的C程序没有编译或给出分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!