当前位置:网站首页>C语言malloc函数的功能及用法
C语言malloc函数的功能及用法
2022-06-27 21:35:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
关于C语言malloc函数
函数介绍
malloc(memory allocation) 中文名称:动态内存分配 用于申请一块连续的指定大小的内存块区域以void*类型返回分配的内存区域地址,当无法知道内存具体位置的时候,想要绑定真正的内存空间,就需要用到动态的分配内存。
应用举例1
关于C语言动态申请数组(整形数据类型)空间的应用
#include<stdio.h>
#include<malloc.h>
void A()
{
int n;//数组元素个数
int *add;
scanf("%d",&n);
add=(int *)malloc(sizeof(int)*n);//内存申请
for(int i=0;i<n;i++)
{
scanf("%d",&add[i]);
}
for(int i=0;i<n;i++)
{
printf("%d",add[i]);
}
}
int main()
{
A();
return 0;
}应用举例2
C语言动态申请数组(结构体)空间的应用
#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)*n);
return n;
}
int Input()//数据输入函数
{
int n=A();
int i,j;
for(i=0;i<n;i++)
{
printf("%d Name:",i+1);
scanf("%s",&I[i].name);
printf("num:\n");
scanf("%d",&I[i].num);
}
return n;
}
void output()//数据输出函数
{
int n=Input();
for(int i=0;i<n;i++)
{
printf("%d |",i);
printf("Name:%s ",I[i].name);
printf("num:%d\n",I[i].num);
}
}
int main()
{
output();
return 0;
}发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/133026.html原文链接:https://javaforall.cn
边栏推荐
- [PCL self study: segmentation4] point cloud segmentation based on Min cut
- [tinyriscv verilator] branch transplanted to Da Vinci development board of punctual atom
- [PCL self study: pclvisualizer] point cloud visualization tool pclvisualizer
- Safe, fuel-efficient and environment-friendly camel AGM start stop battery is full of charm
- [AI application] detailed parameters of NVIDIA geforce RTX 1080ti
- MySQL企业级参数调优实践分享
- c语言字符指针、字符串初始化问题
- [sword finger offer] 47 Maximum value of gifts
- Sell notes | brief introduction to video text pre training
- C language - date formatting [easy to understand]
猜你喜欢

C language character pointer and string initialization

什么是cookie,以及v-htm的安全性隐患

Eliminate gaps around El image images

安全省油環保 駱駝AGM啟停電池魅力十足

Sentinel

【论文阅读|深读】SDNE:Structural Deep Network Embedding
Does the subscription of Siyuan notes stop deleting cloud data directly?
![[digital ic/fpga] detect the position of the last matching sequence](/img/67/a1b575aa9b63892ed585d39e615c58.png)
[digital ic/fpga] detect the position of the last matching sequence

Msp430f5529 MCU reads gy-906 infrared temperature sensor

MYSQL的下载与配置安装
随机推荐
超纲练习题不超纲
Zero foundation self-study SQL course | case function
【AI应用】Jetson Xavier NX的详情参数
【tinyriscv verilator】分支移植到正点原子达芬奇开发板
Feign implements path escape through custom annotations
抓出那些重复的基因
ASP.NET仓库进销存ERP管理系统源码 ERP小程序源码
十大券商注册账户安全吗,会有风险吗?
[AI application] detailed parameters of NVIDIA Tesla v100s-pcie-32gb
Webserver flow chart -- understand the calling relationship between webserver modules
NDSS 2022 received list
MySQL read / write separation configuration
【PCL自学:PCLPlotter】PCLPlotter绘制数据分析图
RecyclerView实现分组效果,多种实现方式
[AI application] detailed parameters of NVIDIA geforce RTX 3060
Golang - the difference between new and make
Count prime [enumeration - > space for time]
零基础自学SQL课程 | IF函数
【AI应用】NVIDIA Tesla V100-PCIE-32GB的详情参数
Chenyun pytorch learning notes_ Build RESNET with 50 lines of code