当前位置: 技术问答>linux和unix
求助:还向各位大哥请教一问题,编译成功了,可是运行时提示:段错误 ,谢谢!
来源: 互联网 发布时间:2015-12-05
本文导语: #include #include #include typedef struct stringdata { char *string; struct stringdata *next; } mydata; void writel(mydata *start); int main() { mydata *start; start->string="starts"; writel(start); return 1; } void writel(mydata...
#include
#include
#include
typedef struct stringdata
{
char *string;
struct stringdata *next;
} mydata;
void writel(mydata *start);
int main()
{
mydata *start;
start->string="starts";
writel(start);
return 1;
}
void writel(mydata *start)
{
mydata *new;
mydata *cur;
char input[100];
cur=start;
while(1)
{
fgets(input,sizeof(input),stdin);
if (strcmp(input,"9"))
{
printf("write quit /n");
break;
}
else
{
new=malloc(sizeof(mydata));
//new=cur->next;
cur->next=new;
new->string=input;
cur=new;
free(new);
}
}
}
编译没有错误,可是不管我在终端是敲入 9 还是别的,
都是提示:
段错误
大哥,我这又是错误在哪里呢?
谢谢!
#include
#include
typedef struct stringdata
{
char *string;
struct stringdata *next;
} mydata;
void writel(mydata *start);
int main()
{
mydata *start;
start->string="starts";
writel(start);
return 1;
}
void writel(mydata *start)
{
mydata *new;
mydata *cur;
char input[100];
cur=start;
while(1)
{
fgets(input,sizeof(input),stdin);
if (strcmp(input,"9"))
{
printf("write quit /n");
break;
}
else
{
new=malloc(sizeof(mydata));
//new=cur->next;
cur->next=new;
new->string=input;
cur=new;
free(new);
}
}
}
编译没有错误,可是不管我在终端是敲入 9 还是别的,
都是提示:
段错误
大哥,我这又是错误在哪里呢?
谢谢!
|
楼主的程序问题很多啊。
指针还的好好学习啊!
#include
#include
#include
typedef struct stringdata
{
char *string;
struct stringdata *next;
} mydata;
mydata *writel(mydata *start);
int main()
{
mydata *start;
start = (mydata *)malloc(sizeof(mydata));
start->string = (char *)malloc(sizeof(char) *8);
strcpy(start->string, "starts");
writel(start);
return 1;
}
mydata *writel(mydata *start)
{
mydata *newp;
mydata *cur = start;
char input[100];
cur=start;
while(1)
{
fflush(stdin);
fgets(input,sizeof(input),stdin);
input[strlen(input) - 1] = '';
if (strcmp(input,"9") == 0)
{
printf("write quitn");
break;
}
else
{
newp = (mydata *)malloc(sizeof(mydata));
newp->string = (char *)malloc(sizeof(char) * (strlen(input)+1));
strncpy(newp->string, input, strlen(input)+1);
cur->next = newp;
cur = newp;
}
}
return start;
}
gcc下可以运行了。
建议楼主好好看看书上的例子,指针到底怎么操作。
指针还的好好学习啊!
#include
#include
#include
typedef struct stringdata
{
char *string;
struct stringdata *next;
} mydata;
mydata *writel(mydata *start);
int main()
{
mydata *start;
start = (mydata *)malloc(sizeof(mydata));
start->string = (char *)malloc(sizeof(char) *8);
strcpy(start->string, "starts");
writel(start);
return 1;
}
mydata *writel(mydata *start)
{
mydata *newp;
mydata *cur = start;
char input[100];
cur=start;
while(1)
{
fflush(stdin);
fgets(input,sizeof(input),stdin);
input[strlen(input) - 1] = '';
if (strcmp(input,"9") == 0)
{
printf("write quitn");
break;
}
else
{
newp = (mydata *)malloc(sizeof(mydata));
newp->string = (char *)malloc(sizeof(char) * (strlen(input)+1));
strncpy(newp->string, input, strlen(input)+1);
cur->next = newp;
cur = newp;
}
}
return start;
}
gcc下可以运行了。
建议楼主好好看看书上的例子,指针到底怎么操作。
|
1)printf("write quit /n"); ===>printf("write quit n");
2)在linux系统中fgets有时候会默认在字符串后面加一个n。即,当你输入“9”,其实是"9n",所以要做处理。
3)start->string="starts";
string是一个char *指针,直接赋值是不安全的,有些编译器过不了。
4)
new=malloc(sizeof(mydata));
//new=cur->next;
cur->next=new;
new->string=input;
cur=new;
free(new);
这一段代码就更看不懂了,你分配了一个结点,但马上又free,这样能构造一个链表吗?
5)
char input[100];
.
.
.
new->string=input;
你在一个函数内申明的数组,当你函数返回时,该变量会从堆栈释放出来,所以
new->string指针得到的肯定是一个错误的指针,或者称为“野指针”
鉴于上面的问题,我建议你将结构体申明为:
typedef struct stringdata
{
char string[MAX_LEN];
struct stringdata *next;
}
在对然后避免用“=”赋值给string,用strcpy();
今天心情不好,你看看:
#include
#include
#include
#define MAX_LEN 20
typedef struct stringdata
{
char string[MAX_LEN];
struct stringdata *next;
}mydata;
void writel (mydata * start);
void printl (mydata *start);
void dell (mydata *start);
int
main ()
{
mydata *start;
start = (mydata *)malloc(sizeof(mydata));
strcpy(start->string , "starts");
start->next = NULL;
writel (start);
printl (start);
dell (start);
start = NULL;
printl (start);
return 1;
}
void
writel (mydata * start)
{
mydata *new;
char input[100];
while (1)
{
fgets (input, sizeof (input), stdin);
input[strlen(input)-1] = '';
if (strcmp (input, "9") == 0)
{
printf ("write quit n");
break;
}
else
{
new = (mydata *)malloc (sizeof (mydata));
strcpy(new->string, input);
new->next = NULL;
start->next = new;
start = start->next;
}
}
}
void printl (mydata *start)
{
while(start != NULL)
{
printf("%sn", start->string);
start = start->next;
}
}
void dell (mydata *start)
{
mydata *p;
p = start;
while (p != NULL)
{
p = p->next; //move next
//free the before node
free(start);
start = NULL;
//point to new node
start = p;
}
}
2)在linux系统中fgets有时候会默认在字符串后面加一个n。即,当你输入“9”,其实是"9n",所以要做处理。
3)start->string="starts";
string是一个char *指针,直接赋值是不安全的,有些编译器过不了。
4)
new=malloc(sizeof(mydata));
//new=cur->next;
cur->next=new;
new->string=input;
cur=new;
free(new);
这一段代码就更看不懂了,你分配了一个结点,但马上又free,这样能构造一个链表吗?
5)
char input[100];
.
.
.
new->string=input;
你在一个函数内申明的数组,当你函数返回时,该变量会从堆栈释放出来,所以
new->string指针得到的肯定是一个错误的指针,或者称为“野指针”
鉴于上面的问题,我建议你将结构体申明为:
typedef struct stringdata
{
char string[MAX_LEN];
struct stringdata *next;
}
在对然后避免用“=”赋值给string,用strcpy();
今天心情不好,你看看:
#include
#include
#include
#define MAX_LEN 20
typedef struct stringdata
{
char string[MAX_LEN];
struct stringdata *next;
}mydata;
void writel (mydata * start);
void printl (mydata *start);
void dell (mydata *start);
int
main ()
{
mydata *start;
start = (mydata *)malloc(sizeof(mydata));
strcpy(start->string , "starts");
start->next = NULL;
writel (start);
printl (start);
dell (start);
start = NULL;
printl (start);
return 1;
}
void
writel (mydata * start)
{
mydata *new;
char input[100];
while (1)
{
fgets (input, sizeof (input), stdin);
input[strlen(input)-1] = '';
if (strcmp (input, "9") == 0)
{
printf ("write quit n");
break;
}
else
{
new = (mydata *)malloc (sizeof (mydata));
strcpy(new->string, input);
new->next = NULL;
start->next = new;
start = start->next;
}
}
}
void printl (mydata *start)
{
while(start != NULL)
{
printf("%sn", start->string);
start = start->next;
}
}
void dell (mydata *start)
{
mydata *p;
p = start;
while (p != NULL)
{
p = p->next; //move next
//free the before node
free(start);
start = NULL;
//point to new node
start = p;
}
}