当前位置:网站首页>Function and usage of malloc function in C language
Function and usage of malloc function in C language
2022-06-28 00:21:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
About C Language malloc function
Function introduction
malloc(memory allocation) Chinese name : Dynamic memory allocation It is used to apply for a continuous memory block area of specified size to void* Type returns the address of the allocated memory area , When you can't know the exact location of memory , Want to bind real memory space , We need to use dynamic memory allocation .
Application, for example, 1
About C Language dynamic application array ( The type of shaping data ) Application of space
#include<stdio.h>
#include<malloc.h>
void A()
{
int n;// Number of array elements
int *add;
scanf("%d",&n);
add=(int *)malloc(sizeof(int)*n);// Memory application
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;
}
Application, for example, 2
C Language dynamic application array ( Structure ) Application of space
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>// contain malloc Function header file
struct Item// Defining structure
{
char name[20];
int num;
};
struct Item *I;// apply Item Pointer to type
int A()// Make a memory request
{
int i,j;
int n;// Number of applications
scanf("%d",&n);
I=(struct Item*)malloc(sizeof(struct Item)*n);
return n;
}
int Input()// Data input function
{
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()// Data output function
{
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;
}
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/133026.html Link to the original text :https://javaforall.cn
边栏推荐
- SCU|通过深度强化学习进行微型游泳机器人的步态切换和目标导航
- [AI application] detailed parameters of NVIDIA Tesla v100s-pcie-32gb
- 【无标题】
- RecyclerView实现分组效果,多种实现方式
- Arduino UNO通过电容的直接检测实现简易触摸开关
- Deployment and test of crtmp live video server
- 吴恩达《机器学习》课程总结(13)_聚类
- MongoDB-在windows电脑本地安装一个mongodb的数据库
- [AI application] detailed parameters of NVIDIA geforce RTX 3060
- Webserver flow chart -- understand the calling relationship between webserver modules
猜你喜欢
Local visualization tool connects to redis of Alibaba cloud CentOS server
Chenyun pytorch learning notes_ Build RESNET with 50 lines of code
现代编程语言:zig
Hcip/hcie Routing & Switching / datacom Reference Dictionary Series (19) comprehensive summary of PKI knowledge points (public key infrastructure)
Character interception triplets of data warehouse: substrb, substr, substring
Alchemy (1): identify prototype, demo and MVP in project development
Webserver flow chart -- understand the calling relationship between webserver modules
zotero文献管理工具安装使用
MySQL企业级参数调优实践分享
flutter系列之:flutter中的变形金刚Transform
随机推荐
Quickly master grep commands and regular expressions
Chapter 2 integrated mp
MATLAB basic function length function
【无标题】
Mise en œuvre du pool de Threads: les sémaphores peuvent également être considérés comme de petites files d'attente
Webserver flow chart -- understand the calling relationship between webserver modules
[AI application] detailed parameters of NVIDIA Tesla v100s-pcie-32gb
flutter系列之:flutter中的变形金刚Transform
Hcip/hcie Routing & Switching / datacom Reference Dictionary Series (19) comprehensive summary of PKI knowledge points (public key infrastructure)
C语言malloc函数的功能及用法
免费、好用、强大的开源笔记软件综合评测
Count prime [enumeration - > space for time]
MongoDB-在windows电脑本地安装一个mongodb的数据库
【论文阅读|深读】SDNE:Structural Deep Network Embedding
線程池實現:信號量也可以理解成小等待隊列
TIME_WAIT过多的解决办法
100 questions for an enterprise architect interview
现代编程语言:Rust (铁锈,一文掌握钢铁是怎样生锈的)
Matlb| optimal configuration of microgrid in distribution system based on complex network
技术的极限(11): 有趣的编程