当前位置:网站首页>Get the variable address of structure member in C language
Get the variable address of structure member in C language
2022-07-05 12:21:00 【A finger leaf next to the giant】
When writing a program , Made a low-level mistake , The main reason is that the concept of value passing and address passing is not well understood . Now I will explain the difference through a small experimental example :
Value passed : When a function passes a value , Formal parameters will first open up new spaces according to their definition types , And re engrave its parameter values into the new memory space , The opening of new memory space also means the change of its storage address , And the operation of its parameters in the function , It is only for the operation in the new space , Not for functions , The transmitted value makes an impact , That is, the two are completely different spaces .
Address delivery : Usually, pointer variables are used as parameters , Because the pointer variable is the variable that saves the address , So when receiving through pointer variables , It's your address , in other words , In memory space , It points to the same memory block , At this time, the operation will be affected at the same time .
demand : In the program , I want to get the address of a structure member variable , And return the address of its member variable by a pointer , By modifying the pointer value , To affect the value of its structure member variables .
C Language :
Wrong way :
#include<stdio.h>// Input and output
typedef struct {
char a[10];
}node, * nodes;
void init(node b, char** m) {
printf("%p\n", &b);
*m = &(b.a[2]);
}
int main() {
node a = {
"123456" };
char* n;
init(a, &n);
*n = '0';
printf("n=%c \na[2]=%c\n", *n, a.a[2]);
printf("%p\n", &a);
printf("%p\n", &(a.a[2]));
printf("%p\n", (&(&a)->a[2]));
printf("%p\n", n);
}

It can be seen from it that :
1. Through the structure object . Get the member variable address and pass the structure pointer -> The way to get the address of the member variable is the same , That is, there is no difference between the two methods .
2. Structure object a Passed to the function as an argument by b receive , But we can see that its memory address is different , This is also related to the mechanism of value transmission .
3.n The address of the member variable returned as a pointer is based on the structure variable after copying b There is ,n and a[2] The address of is different . This leads to the final pair n The value of the pointer is modified , Does not affect its a Structure member variable value .
The right way :
#include<stdio.h>// Input and output
typedef struct {
char a[10];
}node, * nodes;
void init(node* b, char** m) {
printf("b Memory address :%p\n", b);
*m = &(b->a[2]);
}
int main() {
node a = {
"123456" };
char* n;
init(&a, &n);
*n = '0';
printf("n=%c \na[2]=%c\n", *n, a.a[2]);
printf("a Memory address :%p\n", &a);
printf(" &(a.a[2]) Memory address :%p\n", &(a.a[2]));
printf("(&(&a)->a[2]) Memory address :%p\n", (&(&a)->a[2]));
printf("n Memory address :%p\n", n);
}

From the results :
We go through n The pointer gets a[2] The address of , And through the n Modification of the address value pointed to by the pointer , Affected its a[2] Value .
1. From the memory address , Through to a Pointer passing of ( Address delivery ), from b receive , Its a,b The address is the same , then n A pointer to obtain a[2] And return .
边栏推荐
- 【ijkplayer】when i compile file “compile-ffmpeg.sh“ ,it show error “No such file or directory“.
- [untitled]
- Check the debug port information in rancher and do idea remote JVM debug
- PXE启动配置及原理
- HiEngine:可媲美本地的云原生内存数据库引擎
- Master the new features of fluent 2.10
- POJ-2499 Binary Tree
- Hiengine: comparable to the local cloud native memory database engine
- Hash tag usage in redis cluster
- 查看rancher中debug端口信息,并做IDEA Remote Jvm Debug
猜你喜欢

One article tells the latest and complete learning materials of flutter

MySQL index - extended data

16 channel water lamp experiment based on Proteus (assembly language)

查看rancher中debug端口信息,并做IDEA Remote Jvm Debug

Interviewer: is acid fully guaranteed for redis transactions?
你做自动化测试为什么总是失败?

Multi table operation - Auto Association query

Master-slave mode of redis cluster

Error modulenotfounderror: no module named 'cv2 aruco‘

Linux安装部署LAMP(Apache+MySQL+PHP)
随机推荐
Learning JVM garbage collection 06 - memory set and card table (hotspot)
[superhard core] is the core technology of redis
MySQL regular expression
Vscode shortcut key
嵌入式软件架构设计-消息交互
Mmclassification training custom data
POJ-2499 Binary Tree
Application of a class of identities (vandermond convolution and hypergeometric functions)
Just a coincidence? The mysterious technology of apple ios16 is actually the same as that of Chinese enterprises five years ago!
ABAP table lookup program
Why learn harmonyos and how to get started quickly?
IPv6与IPv4的区别 网信办等三部推进IPv6规模部署
Solve the problem of cache and database double write data consistency
How to recover the information server and how to recover the server data [easy to understand]
ZABBIX customized monitoring disk IO performance
Simple production of wechat applet cloud development authorization login
How does MySQL execute an SQL statement?
Two minutes will take you to quickly master the project structure, resources, dependencies and localization of flutter
Take you hand in hand to develop a service monitoring component
【ijkplayer】when i compile file “compile-ffmpeg.sh“ ,it show error “No such file or directory“.