We learnpointerWhen there may be a lot of things so that we confuse so I use a few small experimental program to give you answers to your questions:
First, from the definition, the variable is a pointer to the address stored in the variable。
Unary operator * is the indirection or dereference operator
Unary operator&Take for an object address,It can only be applied to objects in memory
That * (indirection get is a specific value stored in memory)
For pointers experiment
Experiment one
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
[root@localhost ~]# vim zhizhenshiyan1.c #include<stdio.h> main(void) { int x=1; int *ip; ip=&x; printf("十进制显示d ip=%d\n",ip); printf("十进制显示d *ip=%d\n",*ip); printf("二进制显示x ip=%x\n",ip); printf("二进制显示x *ip=%x\n",*ip); } |
The results compiled to
1 2 3 4 5 6 7 8 9 |
[root@localhost ~]# ./a.out 十进制显示d ip=928094692 十进制显示d *ip=1 二进制显示x ip=375199e4 二进制显示x *ip=1 |
A pointer is a variable that holds the address of the variable,In Experiment 1,
ip=&x;
ip address pointing to x to x ip
We will address assigned to the variable x ip
I'm just learning this time in this place is not very deep understanding so there is a test
From the experiments, we can know the difference between a direct addressing and indirect addressing of,I, respectively, in decimal and binary print direct addressing and indirect addressing values,
Direct addressing is printed x address information in memory,Data obtained is directly addressing an address
The indirect addressing is set up on the basis of the direct addressing,Objects Introduction Addressing a value corresponding to the address stored in。
Experiment two
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include<stdio.h> main(void) { int x=1 , y=2; int *ip; printf("int x=1 , y=2;\nint *ip\n"); ip = &x; printf("ip=&x=%d\n",ip); y = *ip; printf("y=*ip=%d\n",y); *ip = 0; printf("*ip=0 x=%d\n",x); printf("&x=%d",&x); } |
———————————————-
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[root@localhost ~]# ./a.out int x=1 , y=2; int *ip ip=&x=1226821388 y=*ip=1 *ip=0 x=0 &x=1226821388 |
—————————————-
The second experiment we can get the following information
First pointer is a variable,Can be changed。
1 2 3 4 5 6 7 |
int x=1 , y=2; *ip=0; printf("*ip=0 x=%d\n",x); printf("&x=%d",&x); |
This tells us a few lines of code to change the assignment of indirect addressing is the address of an object。Before and after the two directly addressed&x The output can be learned
====================
Third experiment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include<stdio.h> main(void) { int z; int x=1 , y=2 ; char a='!' , b='#' ; int *ip , *ab; char *c ,*d; ip = &x; ab = &y; printf("int z\nint x=1,y=2\nint *ip,*ab;\n*ip=&x *ab=&y\n"); *ip=*ip+10; printf("*ip=*ip+10=%d\n",*ip); z = *ip + *ab; printf("z=*ip+*ab=%d\n",z); } |
The compiled results
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[root@localhost ~]# ./a.out int z int x=1,y=2 int *ip,*ab; *ip=&x *ab=&y *ip=*ip+10=11 z=*ip+*ab=13 |
Indirect addressing is the value obtained,Their operation is the value of the arithmetic /
================
Experiment 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include<stdio.h> main(void) { char x=1 , y=2; char *ip; ip=&x; *ip=123; printf("%c",*ip); } |
Results after running
1 2 3 |
[root@localhost ~]# ./a.out { |
In asc code 123 is represented by the symbol”{“
This experiment is very simple to tell us the direct and indirect references cited various relationships,Is a direct reference to the value of the address stored reference value (coding addresses) in asc code 123 is represented by the symbol{Therefore, the output * ip direct reference number 123 the storage location when the represents the symbol”{“.
=====================
ps:In 32-bit systems can use int value pointer value can be expressed even unary mutual,Especially referring asc code
Original article written by LogStudio:R0uter's Blog » About pointers in c language in some of the experiments
Reproduced Please keep the source and description link:https://www.logcg.com/archives/286.html