请高手看一下,为什么会不行,程序如下:
#include "stdio.h"
#include "malloc.h"
#define SIZE 10;
typedef struct node{
char data[10];
struct node *next;
}linknode;
typedef struct queueNode{
struct node *front,*rear;
}linkqueue;
linkqueue *q;
void creat(){
q->front=(linknode *)malloc(sizeof(linknode)); //这句显示出错
};
main(){
creat();
}
我是用netbeans写的c语言程序,编译器是gcc。
程序出输出的结果是 : Segmentation fault
为什么会这样啊。。我在xp里面用turbo c就可以运行。。
是不是ubuntu下的c语言跟xp下的有差别啊。。[/img]
[问题]c语言的小问题
-
- 帖子: 29
- 注册时间: 2008-06-10 17:20
-
- 帖子: 243
- 注册时间: 2005-09-28 9:03
- command
- 帖子: 306
- 注册时间: 2007-10-14 0:50
- 来自: GUCAS
代码: 全选
linkqueue *q;
代码: 全选
#include "stdio.h"
#include "malloc.h"
#define SIZE 10;
typedef struct node{
char data[10];
struct node *next;
}linknode;
typedef struct queueNode{
struct node *front,*rear;
}linkqueue;
linkqueue *q;
void creat(){
q = (linkqueue*)malloc(sizeof(linkqueue));
q->front=(linknode *)malloc(sizeof(linknode)); //这句显示出错
};
main(){
creat();
}
OS: Debian GNU/Linux
Version: lenny
Kernel: 2.6.36
Xorg: 1.4.2
CPU: Intel(R) Core(TM)2 Duo CPU E8400 @ 3.00GHz
HD: 320G SATA
Memory: 2G DDRIII
Graphics:Mobility Radeon HD 3450
Version: lenny
Kernel: 2.6.36
Xorg: 1.4.2
CPU: Intel(R) Core(TM)2 Duo CPU E8400 @ 3.00GHz
HD: 320G SATA
Memory: 2G DDRIII
Graphics:Mobility Radeon HD 3450
-
- 帖子: 21
- 注册时间: 2007-10-17 10:18
可以先别把q声明成指针.这样貌似好理解点!
代码: 全选
#include "stdio.h"
#include "malloc.h"
#define SIZE 10;
typedef struct node{
char data[10];
struct node *next;
}linknode;
typedef struct queueNode{
struct node *front,*rear;
}linkqueue;
linkqueue q;
void creat(linkqueue *q){
q->front=(linknode *)malloc(sizeof(linknode));
};
main(){
creat(&q);
}
-
- 帖子: 2376
- 注册时间: 2006-09-21 14:28