当前位置:网站首页>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
边栏推荐
- Are the registered accounts of the top ten securities companies safe and risky?
- 赛尔笔记|视频文本预训练简述
- [VIM] tutorial, common commands, efficient use of vim editor
- 【tinyriscv verilator】分支移植到正点原子达芬奇开发板
- An analysis of C language functions
- Google Earth engine (GEE) 03 vector data type
- [sword finger offer] 47 Maximum value of gifts
- 通过中金证券经理的开户二维码开股票账户安全吗?还是去证券公司开户安全?
- 证券注册账户安全吗,会有风险吗?
- 支持删除,更新任意结点的优先级队列
猜你喜欢

零基础自学SQL课程 | CASE函数

Sentinel

Zero foundation self-study SQL course | case function

Windows环境下的ELK——Logstash+Mysql(4)

图的存储结构

webService

webserver流程图——搞懂webserver各模块间调用关系

VMware virtual machine bridging connectivity

智慧风电 | 图扑软件数字孪生风机设备,3D 可视化智能运维

Zero foundation self-study SQL course | complete collection of date functions in SQL
随机推荐
An analysis of C language functions
【AI应用】NVIDIA Tesla V100S-PCIE-32GB的详情参数
Zero foundation self-study SQL course | if function
ICML 2022:ufrgs | optimistic linear support and subsequent features as the basis for optimal strategy transfer
VirtualBox extended dynamic disk size pit
【AI应用】NVIDIA GeForce RTX 1080Ti的详情参数
数仓的字符截取三胞胎:substrb、substr、substring
How to find Chinese documents corresponding to foreign documents?
支持删除,更新任意结点的优先级队列
Stream + Nacos
MySQL企业级参数调优实践分享
c语言之字符串数组
零基础自学SQL课程 | SQL基本函数大全
系统学习+主动探索,是最舒适的入门学习方式!
Google Earth Engine(GEE) 03-矢量数据类型
[AI application] detailed parameters of NVIDIA geforce RTX 3060
The file or assembly 'cefsharp.core.runtime.dll' or one of its dependencies could not be loaded. Is not a valid Win32 Application. (exception from hresult:0x800700c1)
Webserver flow chart -- understand the calling relationship between webserver modules
【PCL自学:Segmentation3】基于PCL的点云分割:区域增长分割
[tinyriscv verilator] branch transplanted to Da Vinci development board of punctual atom