当前位置:网站首页>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
边栏推荐
- 一款新型的智能家居WiFi选择方案——SimpleWiFi在无线智能家居中的应用
- Redis's memory elimination mechanism, read this article is enough.
- Why do you always fail in automated tests?
- Learn memory management of JVM 01 - first memory
- Yum only downloads the RPM package of the software to the specified directory without installing it
- Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment
- Matlab boundarymask function (find the boundary of the divided area)
- Read and understand the rendering mechanism and principle of flutter's three trees
- The evolution of mobile cross platform technology
- Mmclassification training custom data
猜你喜欢
Matlab imoverlay function (burn binary mask into two-dimensional image)
Simple production of wechat applet cloud development authorization login
MySQL storage engine
Matlab superpixels function (2D super pixel over segmentation of image)
Seven ways to achieve vertical centering
July Huaqing learning-1
mysql拆分字符串做条件查询
嵌入式软件架构设计-消息交互
[superhard core] is the core technology of redis
A guide to threaded and asynchronous UI development in the "quick start fluent Development Series tutorials"
随机推荐
[pytorch pre training model modification, addition and deletion of specific layers]
Take you hand in hand to develop a service monitoring component
One article tells the latest and complete learning materials of flutter
Want to ask, how to choose a securities firm? Is it safe to open an account online?
MySQL basic operation -dql
Matlab label2idx function (convert the label matrix into a cell array with linear index)
Automated test lifecycle
A guide to threaded and asynchronous UI development in the "quick start fluent Development Series tutorials"
The survey shows that traditional data security tools cannot resist blackmail software attacks in 60% of cases
Course design of compilation principle --- formula calculator (a simple calculator with interface developed based on QT)
Troubleshooting of high memory usage of redis in a production environment
[superhard core] is the core technology of redis
Redis highly available sentinel cluster
信息服务器怎么恢复,服务器数据恢复怎么弄[通俗易懂]
Matlab imoverlay function (burn binary mask into two-dimensional image)
Acid transaction theory
Learning JVM garbage collection 06 - memory set and card table (hotspot)
ZABBIX agent2 installation
MySQL regular expression
What is digital existence? Digital transformation starts with digital existence