执行C程序时出现碎片错误 #包括 结构表 { 字符*IP地址; char*域名; 结构表*下一步; }; 结构表*head=NULL; void add_rec(); void show_rec(); int main() { 添加_rec(); show_rec(); 返回0; } void add_rec() { 结构表*温度=水头; 结构表*temp1=(结构表*)malloc(sizeof(结构表)); 如果(!temp1) printf(“\n无法分配内存\n”); printf(“输入您想要的ip地址\n”); scanf(“%s”,temp1->ipAddress); printf(“\n输入您想要的域名\n”); scanf(“%s”,temp1->domainName); 如果(!temp) { 压头=温度; } 其他的 { while(临时->下一步!=NULL) 温度=温度->下一步; temp->next=temp1; } } 无效显示记录() { 结构表*温度=水头; 如果(!temp) printf(“\n不存在任何条目\n”); while(temp!=NULL) { printf(“ipAddress=%s\t domainName=%s\n”,temp->ipAddress,temp->domainName); 温度=温度->下一步; } }
当我执行此代码并输入第一个节点的IP地址时,我面临着碎片错误。代码崩溃了。有人能开导一下吗?ipAddress只是一个未初始化的字符指针。您尚未分配可由执行C程序时出现碎片错误 #包括 结构表 { 字符*IP地址; char*域名; 结构表*下一步; }; 结构表*head=NULL; void add_rec(); void show_rec(); int main() { 添加_rec(); show_rec(); 返回0; } void add_rec() { 结构表*温度=水头; 结构表*temp1=(结构表*)malloc(sizeof(结构表)); 如果(!temp1) printf(“\n无法分配内存\n”); printf(“输入您想要的ip地址\n”); scanf(“%s”,temp1->ipAddress); printf(“\n输入您想要的域名\n”); scanf(“%s”,temp1->domainName); 如果(!temp) { 压头=温度; } 其他的 { while(临时->下一步!=NULL) 温度=温度->下一步; temp->next=temp1; } } 无效显示记录() { 结构表*温度=水头; 如果(!temp) printf(“\n不存在任何条目\n”); while(temp!=NULL) { printf(“ipAddress=%s\t domainName=%s\n”,temp->ipAddress,temp->domainName); 温度=温度->下一步; } },c,C,当我执行此代码并输入第一个节点的IP地址时,我面临着碎片错误。代码崩溃了。有人能开导一下吗?ipAddress只是一个未初始化的字符指针。您尚未分配可由ipAddress 当你这样做的时候 #include<stdio.h> struct table { char *ipAddress; char *domainName; struct table *next; }; struct table *head = NULL; void add_rec(); voi
ipAddress
当你这样做的时候
#include<stdio.h>
struct table
{
char *ipAddress;
char *domainName;
struct table *next;
};
struct table *head = NULL;
void add_rec();
void show_rec();
int main()
{
add_rec();
show_rec();
return 0;
}
void add_rec()
{
struct table * temp = head;
struct table * temp1 = (struct table *)malloc(sizeof(struct table));
if(!temp1)
printf("\n Unable to allocate memory \n");
printf("Enter the ip address you want \n");
scanf("%s",temp1->ipAddress);
printf("\nEnter the domain name you want \n");
scanf("%s",temp1->domainName);
if(!temp)
{
head = temp;
}
else
{
while(temp->next!=NULL)
temp = temp->next;
temp->next = temp1;
}
}
void show_rec()
{
struct table * temp = head;
if(!temp)
printf("\n No entry exists \n");
while(temp!=NULL)
{
printf("ipAddress = %s\t domainName = %s\n",temp->ipAddress,temp->domainName);
temp = temp->next;
}
}
temp1->ipAddress应该指向可以静态或动态分配的char数组
在你的情况下,你可以改变
scanf("%s",temp1->ipAddress);
到
另外,在通过执行malloc
分配新节点后,您不会初始化新创建节点的next
指针。你应该做:
char ipAddress[16]; // aaa.bbb.ccc.ddd
char domainName[MAX_DOMAIN_LEN]; // choose max length suitably.
此外,当列表最初为空时,head
将为NULL
,因此将执行if块。您应该使head
指向新创建的节点,该节点由temp1
而不是temp
指向:
struct table * temp1 = (struct table *)malloc(sizeof(struct table));
temp1->next = NULL; // this is missing.
我想我可以猜出你所说的碎片错误是什么意思,但你真的应该更具体一些。确切的错误是什么?另外,您是否使用过调试器或类似的工具(我建议使用valgrind),它说了什么?
struct table * temp1 = (struct table *)malloc(sizeof(struct table));
temp1->next = NULL; // this is missing.
if(!temp)
{
head = temp; // this should be head = temp1;
}