当前位置:网站首页>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
边栏推荐
- Technical debt wall: a way to make technical debt visible and negotiable
- [AI application] detailed parameters of NVIDIA geforce RTX 3060
- Is it safe for Huatai Securities to open an account online?
- TIME_ Solutions to excessive wait
- How many securities companies can a person open an account? Is it safe to open an account
- RecyclerView实现分组效果,多种实现方式
- On charsequence
- Customize MySQL connection pool
- Grab those duplicate genes
- 吴恩达《机器学习》课程总结(11)_支持向量机
猜你喜欢

logging日志的使用

How to quote Chinese documents when writing a foreign language?

Alchemy (1): identify prototype, demo and MVP in project development

Summary of wuenda's machine learning course (11)_ Support vector machine

Transmitting and receiving antenna pattern

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

A summer party
![[PCL self study: Segmentation3] PCL based point cloud segmentation: region growth segmentation](/img/9e/f08ce0729c89b0205c0ac47c523ad7.png)
[PCL self study: Segmentation3] PCL based point cloud segmentation: region growth segmentation

吴恩达《机器学习》课程总结(14)_降维

Redis主从复制、哨兵模式、集群的概述与搭建
随机推荐
互联网的发展为产业的变革和转型提供了新的解决方案
Alchemy (4): mental model of programmers
Grab those duplicate genes
内网IP和公网IP的区别及作用
智慧风电 | 图扑软件数字孪生风机设备,3D 可视化智能运维
Introduction to data warehouse
Does the subscription of Siyuan notes stop deleting cloud data directly?
How to select documents for literature review? For example, I can't finish reading more than 200 search results. How to select documents
Flutter series: Transformers in flutter
C语言malloc函数的功能及用法
Request object, response object, session object
[读书摘要] 学校的英文阅读教学错在哪里?--经验主义和认知科学的PK
Sell notes | brief introduction to video text pre training
Oracle数据库的启停
翻译(4): 文本自动完成的匹配规则
股市小白在网上股票开户安全吗?
ValidateRequest=”false” 是做什么的「建议收藏」
Alchemy (1): identify prototype, demo and MVP in project development
Golang uses Mongo driver operation - query (Advanced)
[AI application] detailed parameters of NVIDIA Tesla v100-pcie-32gb