鱼C论坛

 找回密码
 立即注册
查看: 2700|回复: 3

一直不明白链表这个东东,求解

 关闭 [复制链接]
发表于 2011-8-8 17:45:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 小霜 于 2011-8-9 13:04 编辑

甲鱼老师在第十课讲了一个程序。是关于链表的。我很不明白一样东西,求解.' 新手,高手别笑.
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>

#define LEN sizeof(struct student)

struct student *create();
struct student *del(struct student *head , int num );

void print(struct student *head);

struct student
{
    int num;
    float score;
    struct student *next;
};

int n ;

int main(void)
{
  struct student *stu , *p;
  int n;

  stu = create();
    p  = stu;  
    print(p)
}

struct student *create()
{
    ..........
}

请问struct student *create() 是函数吧?怎么不是void 之类做开头的?

另外这个我不明白. 求解

p2 = (struct student *)malloc(LEN)        //#define LEN sizeof(struct student)

struct student* 这样写是干啥的啊 。 我知道是开辟空间,可是,这的意思是开辟struct student 另一个空间??等于开了一个新的结构体?

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-8-8 18:34:09 | 显示全部楼层
struct student *create();
声明一个返回值为指向结构指针的函数!struct student 相当于一个类型名
(struct student *)malloc(LEN)
前边括号为类型转换,把开辟的这一段空间强制转换为一个指向struct student结构的指针!
应该是这样吧!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-8-9 08:52:13 | 显示全部楼层
因为malloc返回的是void*, 要把void*类型的指针赋给struct student*, 就必须要强制类型转换。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2011-8-9 12:54:46 | 显示全部楼层
明白了!谢谢两位{:1_1:}{:1_1:}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-2-8 18:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表