当前位置:网站首页>C language structure is initialized as a function parameter
C language structure is initialized as a function parameter
2022-07-05 12:21:00 【A finger leaf next to the giant】
First : Remember that C Uninitialized variables in language cannot be used , Can't be used as a function parameter , It can be used as a function parameter in the way of address passing .
Preface :
In general , We will extract the code that initializes the structure , Package to init() Function , The initialization is completed by taking the structure variable as the function parameter , But sometimes, for example , Structure variables as parameters , Use... In the initialization function malloc Dynamic memory allocation , Such behavior cannot complete the initialization of external variables . This may lead to the use of uninitialized functions .
Through various experiments , First, we should divide the initialization process in the function into two categories :1. Directly assign values to structural member variables 2. Use malloc Dynamically allocate memory and assign values
1. Directly assign values to structural member variables :
(1).o Is the structure variable , Direct assignment
typedef struct node
{
int h[99];
int m;
}k;
void init(int a,k * p) {
a = 9;
p->m = a;
}
int main() {
int a = 8;
k o ;
init(a,&o);
//c In language , Uninitialized variables , Cannot be used as a parameter
}
here , You can see ,o Is the structure variable , But it's the address , It is feasible to assign a value directly to the address , and a It is value transfer , this , To the initialization function , Its a What you get is just a value . No matter how to modify , Can not affect the outside a value .
So if o Pointer variables for structures ?
(2).o Pointer variables for structs , Direct assignment
typedef struct node
{
int h[99];
int m;
}k;
void init(int a,k * p) {
a = 9;
p->m = a;
}
int main() {
int a = 8;
k* o ;
init(a,o);
//c In language , Uninitialized variables , Cannot be used as a parameter
}
After the modification , The result of the operation is o For uninitialized variables , Cannot be used . So in other words : As a pointer variable, you need to complete initialization first (malloc() or Structure variable assignment ) By using , As a general variable , There is no need to first malloc(), And then assign a value .
of malloc() When is the function used , What are the advantages and disadvantages , It still needs to be understood .
2. Use malloc Dynamically allocate memory and assign values :
(1) o Is the structure variable , Assign value after allocating memory :
#include<stdlib.h> // Dynamic distribution function and random function
typedef struct node
{
int h[99];
int m;
}k;
void init(int a,k * p) {
p=(k*)malloc(sizeof(k));
a = 9;
p->m = a;
}
int main() {
int a = 8;
k o ;
init(a,&o);
//c In language , Uninitialized variables , Cannot be used as a parameter
}
You can find , Its init() After the function ,o Nothing has changed , This is because in the init() Within the function , Original p The pointer stores variables o The address of , After dynamically allocating memory ,p The pointer points to the new ( Variable ) Memory address , This is the same as o Lost contact .
(1) o Pointer variables for structs , Assign value after allocating memory :
#include<stdlib.h> // Dynamic distribution function and random function
typedef struct node
{
int h[99];
int m;
}k;
void init(int a,k ** p) {
*p=(k*)malloc(sizeof(k));
a = 9;
(*p)->m = a;
}
int main() {
int a = 8;
k* o ;
init(a,&o);
//c In language , Uninitialized variables , Cannot be used as a parameter
}
Because uninitialized pointers cannot be used directly as parameters , So we use the secondary pointer to receive , The address of the pointer variable to complete the initialization process .
Fundamentally , A pointer is a variable whose value is a memory address ( Or data objects ), So the secondary pointer is easy to understand .
In terms of the results , The initialization process was successful .
We use secondary pointer p To receive variable pointers o The address of , And then use *p Get its value, that is, the variable pointer o, At this time, we will directly pointer to variables o Dynamic memory allocation . That completes the initialization .
summary :
1.c In language , Whether it's a normal variable , Or pointer variables , It cannot be used directly without initialization , But it can be used by sending addresses .
In the use of malloc() On initialization , Only secondary pointers can be used to pass
Without using malloc() when , It is also divided into value passing and reference, that is, address passing .
2. There are two ways to initialize uninitialized variable functions :
1. For structural variables , Adopt direct assignment
2. Pointer variables to structures , Take secondary pointer parameters , Then dynamically allocate space , assignment
3.malloc() Function usage :
It dynamically allocates memory space , You need to forcibly convert the specified type during allocation , Then return to the first memory address , It is generally received with a pointer of the relevant data type .
Reference documents :
Reference structure
边栏推荐
- GPS data format conversion [easy to understand]
- 自动化测试生命周期
- Interviewer: is acid fully guaranteed for redis transactions?
- One article tells the latest and complete learning materials of flutter
- MySQL transaction
- Tabbar configuration at the bottom of wechat applet
- Video networkState 属性
- Learning JVM garbage collection 06 - memory set and card table (hotspot)
- Matlab struct function (structure array)
- ZABBIX ODBC database monitoring
猜你喜欢
查看rancher中debug端口信息,并做IDEA Remote Jvm Debug
Matlab struct function (structure array)
Redis cluster (master-slave) brain crack and solution
Learn the memory management of JVM 02 - memory allocation of JVM
Course design of compilation principle --- formula calculator (a simple calculator with interface developed based on QT)
Hiengine: comparable to the local cloud native memory database engine
mmclassification 训练自定义数据
Four operations and derivative operations of MATLAB polynomials
MySQL splits strings for conditional queries
Use and install RkNN toolkit Lite2 on itop-3568 development board NPU
随机推荐
【ijkplayer】when i compile file “compile-ffmpeg.sh“ ,it show error “No such file or directory“.
Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment
Tabbar configuration at the bottom of wechat applet
Matlab label2idx function (convert the label matrix into a cell array with linear index)
MySQL installation, Windows version
How to recover the information server and how to recover the server data [easy to understand]
Complete activity switching according to sliding
Get all stock data of big a
图像超分实验:SRCNN/FSRCNN
ZABBIX agent2 installation
Semantic segmentation experiment: UNET network /msrc2 dataset
SENT协议译码的深入探讨
Four operations and derivative operations of MATLAB polynomials
Redis highly available sentinel cluster
HiEngine:可媲美本地的云原生内存数据库引擎
【load dataset】
查看rancher中debug端口信息,并做IDEA Remote Jvm Debug
The evolution of mobile cross platform technology
How can beginners learn flutter efficiently?
Linux安装部署LAMP(Apache+MySQL+PHP)