当前位置:网站首页>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 .
边栏推荐
- Third party payment interface design
- Video networkstate property
- A new WiFi option for smart home -- the application of simplewifi in wireless smart home
- Multi table operation - sub query
- 只是巧合?苹果 iOS16 的神秘技术竟然与中国企业 5 年前产品一致!
- ZABBIX 5.0 - LNMP environment compilation and installation
- Sentinel sentinel mechanism of master automatic election in redis master-slave
- Open3d mesh (surface) coloring
- The survey shows that traditional data security tools cannot resist blackmail software attacks in 60% of cases
- Learning JVM garbage collection 06 - memory set and card table (hotspot)
猜你喜欢
Simple production of wechat applet cloud development authorization login
Wireless WiFi learning 8-channel transmitting remote control module
How to clear floating?
Take you hand in hand to develop a service monitoring component
Troubleshooting of high memory usage of redis in a production environment
MySQL transaction
mysql拆分字符串做条件查询
How can beginners learn flutter efficiently?
Error modulenotfounderror: no module named 'cv2 aruco‘
The survey shows that traditional data security tools cannot resist blackmail software attacks in 60% of cases
随机推荐
Understand redis persistence mechanism in one article
Troubleshooting of high memory usage of redis in a production environment
1. Laravel creation project of PHP
Wireless WiFi learning 8-channel transmitting remote control module
JS for loop number exception
Redis highly available slice cluster
Matlab struct function (structure array)
Deep discussion on the decoding of sent protocol
Codeforces Round #804 (Div. 2)
byte2String、string2Byte
What is the difference between canvas and SVG?
The evolution of mobile cross platform technology
Swift - add navigation bar
16 channel water lamp experiment based on Proteus (assembly language)
MySQL trigger
[superhard core] is the core technology of redis
About cache exceptions: solutions for cache avalanche, breakdown, and penetration
Read and understand the rendering mechanism and principle of flutter's three trees
GPS數據格式轉換[通俗易懂]
Flutter2 heavy release supports web and desktop applications