标题:这个链表为什么会段错误
只看楼主
wll19901105
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2012-12-4
结帖率:100%
已结贴  问题点数:8 回复次数:2 
这个链表为什么会段错误
        temp->next = head;
        return head;
}

int main()
{
        struct student *head = NULL;
        struct student *q = NULL;
        q = add(head,23,"wang");
        while(q != NULL)
        printf("%s %d",q->name,q->age);
}
搜索更多相关主题的帖子: head next return 
2012-12-14 21:27
wll19901105
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2012-12-4
得分:0 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct student
{
    int age;
    char name[10];
    struct student *next;   
};

struct student *add(struct student *head,int age,char *name)
{
    struct student *p = malloc(sizeof(struct student));
    struct student *temp = NULL;
    temp = head;
    while(temp->next != NULL)
        temp = temp->next;
    strcpy(p->name,"name");
    p->age = age;
    p->next = NULL;
    temp->next = head;
    return head;
}

int main()
{
    struct student *head = NULL;
    struct student *q = NULL;
    q = add(head,23,"wang");
    while(q != NULL)
    printf("%s %d",q->name,q->age);
}
2012-12-14 21:28
fu2751653
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:38
专家分:100
注 册:2011-4-11
得分:8 
struct student *p = malloc(sizeof(struct student));malloc用法有问题
struct student *p =(struct student*)malloc(sizeof(struct student));
 strcpy(p->name,"name");这个地方也有问题吧,还有几个
2012-12-14 21:58



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-391291-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.389312 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved