当前位置:网站首页>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
边栏推荐
- 炼金术(8): 开发和发布的并行
- flutter系列之:flutter中的变形金刚Transform
- 本地可视化工具连接阿里云centOS服务器的redis
- Validaterequest= "false" is a "suggestion collection" for what
- Is the securities registration account safe? Is there any risk?
- [digital ic/fpga] detect the position of the last matching sequence
- Logging log usage
- Redis主从复制、哨兵模式、集群的概述与搭建
- [AI application] detailed parameters of NVIDIA geforce RTX 1080ti
- Code neatness -- function
猜你喜欢
![Using two stacks to implement queues [two first in first out is first in first out]](/img/de/07297816f1a44d41389bb45d012c80.png)
Using two stacks to implement queues [two first in first out is first in first out]

Chenyun pytorch learning notes_ Build RESNET with 50 lines of code
![[读书摘要] 学校的英文阅读教学错在哪里?--经验主义和认知科学的PK](/img/7b/8b3619d7726fdaa58da46b0c8451a4.png)
[读书摘要] 学校的英文阅读教学错在哪里?--经验主义和认知科学的PK

Introduction to data warehouse

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

剑指 Offer 65. 不用加减乘除做加法

Arduino uno realizes simple touch switch through direct detection of capacitance

Sentinel

Msp430f5529 MCU reads gy-906 infrared temperature sensor

flutter系列之:flutter中的变形金刚Transform
随机推荐
一个人可以到几家证券公司开户?开户安全吗
[黑苹果系列] M910x完美黑苹果系统安装教程 – 2 制作系统U盘-USB Creation
Alchemy (9): simple but not simple, never-ending test -- always_ run
The development of the Internet provides new solutions for industrial transformation
[untitled]
ValidateRequest=”false” 是做什么的「建议收藏」
Flutter series: Transformers in flutter
How to find Chinese documents corresponding to foreign documents?
After a period of silence, I came out again~
VirtualBox extended dynamic disk size pit
安全省油環保 駱駝AGM啟停電池魅力十足
SQL报了一个不常见的错误,让新来的实习生懵了
炼金术(4): 程序员的心智模型
翻译(4): 文本自动完成的匹配规则
MYSQL的下载与配置安装
Promise是什么
Pat class B 1013
Sell notes | brief introduction to video text pre training
哪个证券公司炒股开户是比较好,更安全靠谱的
HCIP/HCIE Routing&Switching / Datacom备考宝典系列(十九)PKI知识点全面总结(公钥基础架构)