当前位置:网站首页>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
边栏推荐
- 使用cef3开发的浏览器不支持flash问题的解决
- [PCL self study: segmentation4] point cloud segmentation based on Min cut
- 零基础自学SQL课程 | CASE函数
- 【tinyriscv verilator】分支移植到正点原子达芬奇开发板
- [digital ic/fpga] detect the position of the last matching sequence
- What if Fiddler fails to listen to the interface
- Google Earth engine (GEE) 03 vector data type
- Excel print settings public header
- How to use the apipost script - global variables
- RecyclerView实现分组效果,多种实现方式
猜你喜欢
![[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

【tinyriscv verilator】分支移植到正点原子达芬奇开发板

Excel print settings public header

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

本地可视化工具连接阿里云centOS服务器的redis

【PCL自学:PCLPlotter】PCLPlotter绘制数据分析图

2022 PMP project management examination agile knowledge points (3)
![计数质数[枚举 -> 空间换时间]](/img/11/c52e1dfce8e35307c848d12ccc6454.png)
计数质数[枚举 -> 空间换时间]

Cornernet understands from simple to profound

C language character pointer and string initialization
随机推荐
刚开始看英文文献,想问一下各位,最初应该怎么看进去?
【Try to Hack】veil-evasion免杀
[AI application] detailed parameters of NVIDIA Tesla v100s-pcie-32gb
【PCL自学:PCLPlotter】PCLPlotter绘制数据分析图
Swing UI container (I)
Can you do these five steps of single cell data cleaning?
Grab those duplicate genes
halcon之区域:多种区域(Region)特征(6)
Solve the cross domain problem of the new version of chrome: Cookie loss and samesite attribute problem "recommended collection"
什么是cookie,以及v-htm的安全性隐患
安全省油环保 骆驼AGM启停电池魅力十足
沉寂了一段时间 ,我又出来啦~
c语言-日期格式化[通俗易懂]
零基础自学SQL课程 | SQL基本函数大全
Zero foundation self-study SQL course | if function
Elk in Windows Environment - logstash+mysql (4)
How to use raspberry pie (and all kinds of pies)
通过中金证券经理的开户二维码开股票账户安全吗?还是去证券公司开户安全?
股市小白在网上股票开户安全吗?
golang使用mongo-driver操作——查(数组相关)