The definition of pointer variables is similar to that of general variables, and its form is as follows:
data type [memory type 1] * [memory type 2] identifier;
[memory type 1] means it is defined as a memory-based pointer, and when this option is not available, it is defined as a general pointer. The difference between these two pointers is that they have different storage bytes. A general pointer occupies three bytes in memory, the first byte stores the encoding of the pointer memory type (determined by the default value of the compilation mode at compile time), and the second and third bytes store the high and low address offsets of the pointer respectively. The encoding values of the memory type are as follows:
Storage Type I | Idata/data/bdata | xdata | pdata | Code |
Encoded value | 0x00 | 0x01 | 0xFE | 0xFF |
[Storage type 2] is used to specify the memory space of the pointer itself.
-
char * c_ptr; int * i_ptr; long * l_ptr;
The above definition is a general pointer. c_ptr points to a char variable. Where is this char variable located? This is related to the default value of the compilation mode during compilation.
If Menory Model—Variable—Large: XDATA, then this char variable is located in the xdata area:
If Menory Model—Variable—Compact: PDATA, then this char variable is located in the pdata area:
If Menory Model——Variable——Small: DATA, then this char variable is located in the data area.
The pointers c_ptr, i_ptr, and l_ptr variables themselves are located in the on-chip data storage area. -
char * data c_ptr; int * idata i_ptr; long * xdata l_ptr;
According to the above definition, the c_ptr, i_ptr, l_ptr variables themselves are located in the data, idata, xdata areas respectively. -
char data * c_ptr; // indicates that it points to a char type variable in the data area, and c_ptr is in the on-chip storage area;
int xdata * i_ptr; // indicates that it points to an int type variable in the xdata area, and i_ptr is in the on-chip storage area;
long code * l_ptr; // indicates that it points to a long type variable in the code area, and l_ptr is in the on-chip storage area; -
char data * data c_ptr; // indicates that it points to a char type variable in the data area, and c_ptr is in the on-chip storage area data;
Int xdata * idata i_ptr; // indicates that it points to an int type variable in the xdata area, and i_ptr is in the off-chip storage area xdata;
long code * xdata l_ptr; // indicates that it points to a long type variable in the code area, and l_ptr is in the on-chip storage area xdata;
2. Pointer Application
-
int x, j;
int * px, *py;
px=&x; py=&y; - *px=0; py=px;
- *px++<=>*(px++)
- (*px)++<=>x++
-
unsigned char xdata * x;
unsinged char xdata * y;
x=0x0456;
*x=0x34 //Equivalent to mov dptr,#456h; mov a,#34h; movx @dptr,a -
unsigned char pdata * x;
x=0x045;
*x=0x34 //equivalent to mov r0,#45h; mov a,#34h; movx @r0,a -
unsigned char data * x;
x=0x30;
*x=0x34 //equivalent to mov a,#34h; mov 30h ,a -
int *px;
px=(int xdata *)0x4000; //Assign the xdata type pointer 0x4000 to px, that is, force 0x4000 to be converted into a pointer to the int type variable in the xdata area and assign it to px. -
int x;
x=*((char xdata *)0x4000); //Force 0x4000 to be converted into a pointer to an int variable in the xdata area, and take the value from this address and assign it to variable x. - px=*((int xdata * xdata *)0x4000); //How to analyze?
- px=*( (int xdata * xdata *)0x4000); covers the shaded part, which means that 0x4000 is forced to be converted into a pointer to an X-type variable in the xdata area. This X-type variable is the shaded "int xdata *", that is, the variable type pointed to by 0x4000 is a pointer to an int-type variable in the xdata area, that is, 0x4000 contains another pointer, which points to an int-type variable in the xdata area. The Px value is the pointer in 0x4000. For example, [0x4000] - [0x2000] - 0x34. Px = 0x2000.
- x=**((int xdata * xdata *)0x4000); x contains the value pointed to by the pointer in 0x4000. For example, [0x4000] - [0x2000] - 0x34.
3. Pointers and arrays
-
int arr[10];
int * pr;
pr=arr; // equivalent to pr=&arr[0];
in this case, *(pr+1)==arr[1]; *(pr+2)==arr[2]; *(arr+3)==arr[3]; *(arr+4)==arr[4];
or pr[0],pr[1]…. represents arr[0],arr[1]…..
can use *pr++ (equivalent to *(pr++)) to access all array elements, but *arr++ is not possible. Because arr is a constant and cannot be ++ operated -
char * s1
char code str[]=”abcdefg”
s1=str; - char *s1=”abcdefg”;
4. Pointers and structures
-
typedef struct _data_str {
unsigned int DATA1[10];
unsigned int DATA2[10]
;
unsigned int DATA3[10]; unsigned int DATA4[10];
unsigned int DATA5[10];
unsigned int DATA6[10];
unsigned int DATA7[10];
unsigned int DATA8[10];
}DATA_STR;
//Open up an external RAM space, make sure this space is enough to hold the xdata you need
uchar my_data[MAX_STR] _at_ 0x0000;
DATA_STR *My_Str;
My_Str=(DATA_STR*)my_data; //Point your structure pointer to the beginning of this array
The following operations are like this:
My_Str->DATA1[0]=xxx;
My_Str->DATA1[1]=xxx;
then your variable will be put into XDATA naturally.
Note that my_data[MAX_STR] defined cannot be operated casually, it is only used to allocate memory at the beginning. -
struct student
{
char name[20];
int num;
}stu1,stu2; -
struct student
{
char name[20];
int num;
};
struct student stu1,stu2;
struct student *p;
p=&stu1;
Methods for accessing members:
A. stu1.num
B. (*p).num; //Because the priority of "." is higher than that of "*", brackets must be added.
C. P->num; -
struct student stu[10];
struct student * p;
p=stu;
Previous article:LCD touch screen technology and application designed with C8051F023
Next article:Application of C8051F064 single chip microcomputer in remote measurement and control device
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Check in, welcome to communicate
- China's first batch of autonomous driving unmanned commercial licenses issued! Do you dare to sit in an unmanned car? ?
- GDB debugging and DSP
- The IO pin of MSP430 is set as input, but it receives the output signal of the Hall sensor, and the chip-side signal does not switch.
- [Serial] [Starlight Lightning STM32F407 Development Board] Chapter 10 Serial Communication Experiment
- Where do errors in vector network analyzers come from?
- How to use a wiring harness tester to measure small resistance? Here's a method
- Those who don't know about RF MEMS, please take a look. This is what the technology is like.
- General Design Considerations for Embedded Systems
- Download the NI white paper "Overcoming the Challenges of Production Test for Complex Devices Under Test" for free and get a chance to win prizes!