1.malloc()函数动态分配一段存储空间
(1)函数原型:void *malloc(unsigned size);
(2)调用格式:malloc(size)
(3)功能:在内存分配一个size字节的存储区。调用结果为新分配的存储区的首地址,是一个void类型指针。
#include<stdio.h>
#include<stdlib.h>
struct st
{
int n;
struct st *next;
};
int main(void)
{
struct st *p;
p=(struct st*)malloc(sizeof(struct st));//将函数的返回值转换为结构体指针
p->n=5;
p->next=NULL;
printf("p->n=%d\tp->next=%x\n",p->n,p->next);
return 0;
}
结果为:
p->n=5 p->next=0