| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 390 人关注过本帖
标题:这个链表为什么会段错误
只看楼主 加入收藏
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
快速回复:这个链表为什么会段错误
数据加载中...
 
   



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

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