试图从结构中打印一个字符指针,但我在 C 中遇到了分段错误
trying to printf a char pointer from a struct but I get a segmentation fault in C
我正在用结构做一些工作,由于某种原因,当我尝试 运行 我的程序时出现分段错误。如果我删除 printfunction 中的 printf 行,程序将毫无问题地完成,但重点是打印出存储在 list->line 中的内容。
addLine 函数添加一个新的结构对象,将其放在列表的末尾并指向第一个结构对象。
printFunction 应该打印出列表中的所有行。
结构:
typedef struct listoflines list;
struct listoflines {
list* next;
char* line;
};
addLine函数:
void addLine(list* firstline, char* linep) {
list* newline = malloc(sizeof(list));
newline->next = firstline;
newline->line = malloc((sizeof(char) * strlen(linep) + 1));
strcpy(newline->line, linep);
list* currentline = firstline;
while(currentline->next){
if(currentline->next == firstline){
currentline->next = newline;
return;
}
else {
currentline = currentline->next;
}
}
currentline->next = newline;
}
printFunction函数:
void printFunction(list* firstline){
list* currentline = firstline;
while(currentline->next != firstline){
printf("%s\n", currentline->line);
currentline = currentline->next;
}
}
addLine 函数已 运行 4 次,printFunction 被赋予指向与 addline 函数相同的 "firstline" 的指针。
非常感谢任何帮助。在这一点上我不知道会出什么问题。
由于您使用全零初始化 firstline
,因此在 while 循环的第一次迭代中,currentline->line
将为 NULL,并且在打印该行时出现段错误。您可以使用 gdb
.
轻松调试此类错误
list* currentline = firstline;
while(currentline->next != firstline){
printf("%s\n", currentline->line); /* the string here will be NULL */
currentline = currentline->next;
}
关于您的实现的一个小建议:您可以使用这样的结构来使您的程序更具可读性:
struct text {
list *firstline;
list *lastline;
}
然后创建该结构的实例并传递它而不是传递 firstline
。这样,文本中没有任何行会更加明显(firstline
将是 NULL
)。您也可以摆脱 addLine()
中的 while
循环,因为您已经维护了指向最后一行的指针。
此外,您可以使用 string.h
中的 strdup()
创建字符串的新副本,而不是 malloc()
和 strcpy()
。
我正在用结构做一些工作,由于某种原因,当我尝试 运行 我的程序时出现分段错误。如果我删除 printfunction 中的 printf 行,程序将毫无问题地完成,但重点是打印出存储在 list->line 中的内容。
addLine 函数添加一个新的结构对象,将其放在列表的末尾并指向第一个结构对象。
printFunction 应该打印出列表中的所有行。
结构:
typedef struct listoflines list;
struct listoflines {
list* next;
char* line;
};
addLine函数:
void addLine(list* firstline, char* linep) {
list* newline = malloc(sizeof(list));
newline->next = firstline;
newline->line = malloc((sizeof(char) * strlen(linep) + 1));
strcpy(newline->line, linep);
list* currentline = firstline;
while(currentline->next){
if(currentline->next == firstline){
currentline->next = newline;
return;
}
else {
currentline = currentline->next;
}
}
currentline->next = newline;
}
printFunction函数:
void printFunction(list* firstline){
list* currentline = firstline;
while(currentline->next != firstline){
printf("%s\n", currentline->line);
currentline = currentline->next;
}
}
addLine 函数已 运行 4 次,printFunction 被赋予指向与 addline 函数相同的 "firstline" 的指针。 非常感谢任何帮助。在这一点上我不知道会出什么问题。
由于您使用全零初始化 firstline
,因此在 while 循环的第一次迭代中,currentline->line
将为 NULL,并且在打印该行时出现段错误。您可以使用 gdb
.
list* currentline = firstline;
while(currentline->next != firstline){
printf("%s\n", currentline->line); /* the string here will be NULL */
currentline = currentline->next;
}
关于您的实现的一个小建议:您可以使用这样的结构来使您的程序更具可读性:
struct text {
list *firstline;
list *lastline;
}
然后创建该结构的实例并传递它而不是传递 firstline
。这样,文本中没有任何行会更加明显(firstline
将是 NULL
)。您也可以摆脱 addLine()
中的 while
循环,因为您已经维护了指向最后一行的指针。
此外,您可以使用 string.h
中的 strdup()
创建字符串的新副本,而不是 malloc()
和 strcpy()
。