当前位置:网站首页>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
边栏推荐
- Using two stacks to implement queues [two first in first out is first in first out]
- 十大券商注册账户安全吗,会有风险吗?
- 超纲练习题不超纲
- 系统学习+主动探索,是最舒适的入门学习方式!
- 虽然TCGA数据库有33种癌症
- Detailed explanation of MATLAB axis coordinate axis related settings
- 思源笔记订阅停止直接删云端数据嘛?
- ICML 2022: UFRGS |作为最优策略转移基础的乐观线性支持和后继特征
- 搭建开源美观的数据库监控系统-Lepus
- How to solve the problem that the browser developed with CeF3 does not support flash
猜你喜欢

Webserver flow chart -- understand the calling relationship between webserver modules

Transmitting and receiving antenna pattern

Applet referer

Storage structure of graph
![[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
![[PCL self study: pclvisualizer] point cloud visualization tool pclvisualizer](/img/38/c7ce908bfcc5cc5cd5856996aa015b.png)
[PCL self study: pclvisualizer] point cloud visualization tool pclvisualizer

Zero foundation self-study SQL course | if function

安全省油环保 骆驼AGM启停电池魅力十足

webService
Does the subscription of Siyuan notes stop deleting cloud data directly?
随机推荐
NDSS 2022 received list
flutter系列之:flutter中的变形金刚Transform
解决新版chrome跨域问题:cookie丢失以及samesite属性问题「建议收藏」
[PCL self study: pclvisualizer] point cloud visualization tool pclvisualizer
How to use the apipost script - global variables
c语言字符指针、字符串初始化问题
Golang uses Mongo driver operation - query (basic)
十大券商注册账户安全吗,会有风险吗?
golang使用mongo-driver操作——查(基础)
如何找到外文文献对应的中文文献?
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)
安全省油環保 駱駝AGM啟停電池魅力十足
本地可视化工具连接阿里云centOS服务器的redis
[idea] idea formatting code skills
At the beginning of reading English literature, I would like to ask you how you should read it in the first place?
It supports deleting and updating the priority queue of any node
【PCL自学:PCLPlotter】PCLPlotter绘制数据分析图
第 2 章 集成 MP
MySQL分表查询之Merge存储引擎实现
One step forward is excellent, one step backward is ignorant