77问答网
所有问题
C语言 链表中malloc 用处: 比如head=(struct student *)malloc(sizeof(struct student)); 望大虾指教··
如题所述
举报该问题
推荐答案 2011-03-10
就是分配空间呀。
下面我将按步骤给你讲解:
sizeof(struct student) //获得结点所需空间的大小
malloc(sizeof(struct student)) ; //在堆中分配空间
(stuct student *)malloc(sizeof(struct student)); //将malloc返回值转换为指向链表结点的指针
//因为malloc的返回值为void *
温馨提示:答案为网友推荐,仅供参考
当前网址:
http://77.wendadaohang.com/zd/Y8IYGv33q.html
其他回答
第1个回答 推荐于2018-04-13
申请一块大小为 sizeof(struct student) 的内存并返回这块内存的首地址 (struct student *)是强制类型转换 将其首地址转换为结构体类型 head=(struct student *)malloc(sizeof(struct student)); 是让链表的头指针指向这块内存的首地址
本回答被提问者和网友采纳
相似回答
...
链表
p
=(struct
stu
*)malloc(sizeof(struct
stu))为什么新开辟
的
空 ...
答:
malloc 分配一段空间,其参数是空间的大小(单位是字节
)
比如malloc(
5),是分配一段五个字节大小的空间。其返回值的类型是void *。因此赋值的时候要经过强制类型转换
sizeof()
是计算参数所占的空间大小。如果参数是一个类型,那就是这种类型的数据的大小,如果是一个变量,那就是这个变量的大小。那...
...node
*)malloc(sizeof(struct
node)); ptr
=head
;是什么有什么
作用
_百 ...
答:
malloc(
n)函数是动态分配n字节的内存空间。函数返回值是void型的所分配空间的首地址。你上面
的head
应该定义的是struct node类型的指针,所以把函数返回值赋给head要用(struct node
*)
进行强制类型转换。
sizeof(struct
node)是结构体node所需的字节数。head一般是作为表头指针,ptr
=head
;应该就是用ptr保留...
p1 = p2
= (struct
student
*)malloc(sizeof(struct
student)); 这个语...
答:
新建一个student数据结构的对象,为其分配student结构所占用的内存空间。size
of
(struct student)为求该对象在内存中占用多少内存空间,让后用malloc函数分配同样大小的空间。将指针p1,p2指向该对象,即新分配出的空间。
c语言
问题
(student*)malloc(sizeof(student
))?
答:
include<stdio.h>#include<stdlib.h>#include<malloc.h>//包含
malloc函数的
头文件struct Item//定义结构体{char name[20];int num;};struct Item *I;//申请Item类型的指针int A()//进行内存申请{int i,j;int n;//申请的数量scanf("%d",&n);I
=(struct
Item
*)malloc(sizeof(struct
Item)...
c链表
解释
答:
struct
student
*
head
; //结构体指针 struct student *p1,*p2; //功能是:创建两个学生信息
的结构体
,其中p1用于用户输入信息,p2用于保存用户输入的信息 n=0;p1=p2
=(struct
student
*) malloc(
LEN); //为p1、p2动态分配空间 scanf("%ld,%f",&p1->num,&p1->source); //输入p1...
C语言链表
问题
答:
前面
的(struct
student*)
是强制类型转换,其实什么也没做,仅仅是显式的通知编译系统,允许把这个新内存的首地址当做是
结构体
地址,用它来给p赋值
malloc
分到的内存是一个没有指向特定类型(void
*)的
指针,不用强制转换时 编译器禁止对不同类型的指针相互赋值,所以malloc调用后几乎总是在前面有个强制...
求
C语言
数据结构中
的链表
创建,插入和删除代码
答:
head=(STU
*)malloc(sizeof(struct
student
)); head->next=NULL; temp=p2=head; while(! feof(fp)) { p1=(STU *)malloc(sizeof(struct student)); temp=p2; p2->next=p1; p2=p1; fscanf(fp,"%d%s%d%f",p1->number,p1->name,p1->sum,p1->average); for(i=0;i<COURSE_NUM;i++) fscanf...
大家正在搜
链表head指针指向什么
其中head指向链表首结点
在一个单链表head中
设一条单链表的头指针为head
malloc debug
malloc_trim
链表c语言
链表的head是什么
链表的head到底是什么
相关问题
head=p1=(struct stu *)malloc(s...
C语言怎么理解链表 p=(struct stu *)mall...
c语言这里的p1=p2=(struct student *)...
c语言 pEnd=pNew=(struct Student*...
pEnd=pNew=(struct Student*)mal...
初学者问一个C语言的问题,关于链表。
C语言链表求元素个数
c语言定义一个结构体指针变量跟定义该指针变量之后再调用mal...