Definition and Application of Pointer Variable of C51 SCM
one. Definition of a pointer variable
The definition of a pointer variable is similar to that of a general variable, and its form is as follows:
data type [memory type 1] * [memory type 2] identifier;
[Memory Type 1] Indicates that it is defined as a memory-based pointer. Without this option, it is defined as a general pointer. The difference between these two kinds of pointers is that they are stored in different bytes. Generally, a pointer occupies three bytes in memory. The first byte stores the code of the memory type of the pointer (determined by the default value of the compilation mode at compile time), and the second and third bytes respectively store the high-order and high-order bits of the pointer. Lower address offset. The coded values ​​for the memory types are as follows:
Storage type IIdata/data/bdataxdatapdataCode encoded value 0x000x010xFE0xFF
[Storage type 2] is used to specify the memory space of the pointer itself.
1,
char *c_ptr;
int *i_ptr;
long *l_ptr;
The above definition is a general pointer, and c_ptr points to a char variable, so where is this char variable located? This is related to the default value of the compilation mode at compile time,
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.
2,
char *data c_ptr;
int * idata i_ptr;
long *xdata l_ptr;
In the above definition, the c_ptr, i_ptr, and l_ptr variables are located in the data, idata, and xdata areas, respectively.
3.
char data * c_ptr; //Indicates that it points to a char 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 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 variable in the code area, and l_ptr is in the on-chip storage area;
4.
char data * data c_ptr; //Indicates that it points to a char 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 variable in the xdata area, and i_ptr is in the on-chip storage area idata;
long code * xdata l_ptr; //Indicates that it points to a long variable in the code area, and l_ptr is in the off-chip storage area xdata;
two. pointer application
1,
int x, j;
int *px, *py;
px=&x;
py=&y;
2,
*px=0;
py=px;
*px++ "=" * (px++)
(*px)++ "=" x++
copy code
unsigned char xdata * x;
unsinged char xdata * y;
x=0x0456;
*x=0x34
//equivalent to mov dptr, #456h
// mov a, #34h
// movx @dptr, a
copy code
6.
copy code
unsigned char pdata *x;
x=0x045;
*x=0x34
//equivalent to mov r0, #45h
// mov a, #34h
// movx @r0, a
copy code
7.
copy code
unsigned char data * x;
x=0x30;
*x=0x34
//equivalent to mov a, #34h
// mov 30h, a
copy code
8,
int *px;
px = (int xdata *) 0x4000;
//Assign the xdata type pointer 0x4000 to px, that is, cast 0x4000 to a pointer to the int type variable in the xdata area, and assign it to px.
9,
int x;
x=*((char xdata*)0x4000);
//Force 0x4000 to a pointer to an int variable in the xdata area, and take the value from this address and assign it to the variable x.
10.
px=*((int xdata*xdata*)0x4000); //How to analyze?
11.
px=*((int xdata * xdata *) 0x4000); // Cover the shadow part, which means to cast 0x4000 to a pointer to the X-type variable in the xdata area, this X-type variable is the shadow "int xdata * ",
//That is, the variable type pointed to by 0x4000 is a pointer to an int variable in the xdata area, that is, another pointer is placed in 0x4000, and this pointer points to an int variable in the xdata area.
//The Px value is placed in the pointer placed in 0x4000. For example [0x4000] - [0x2000] - 0x34. *Px=0x2000.
12.
x=**((int xdata * xdata *) 0x4000); //The value pointed to by the pointer placed in 0x4000 is placed in x. For example [0x4000] - [0x2000] - 0x34.
three. pointers and arrays
1,
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]…. .
*pr++ (equivalent to *(pr++)) can be used to access all array elements, while *arr++ cannot. Because arr is a constant, it cannot be operated by ++
2,
char *s1
char code str[] = "abcdefg"
s1=str;
3.
char *s1=â€abcdefgâ€;
Four. pointers and structures
1,
copy code
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 install what you need
xdata 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
copy code
The subsequent operation is like this:
My_Str->DATA1[0]=xxx;
My_Str->DATA1[1]=xxx;
Then your variables are naturally placed in XDATA.
Note: The defined my_data[MAX_STR] cannot be manipulated casually, it is only used to open up memory at the beginning.
2,
copy code
struct student
{
char name[20];
int num;
}stu1,stu2;
copy code
3.
copy code
struct student
{
char name[20];
int num;
};
struct student stu1, stu2;
struct student *p;
p=&stu1;
copy code
Access member methods:
A. stu1.num
B. (*p).num; //Because "." has a higher priority than "*" so add parentheses.
C. P->num;
4.
struct student stu[10];
struct student *p;
p=stu;
CAT-1 Personal GPS Trackers,2-way communication GPS trackers,GPS trackers with voice,GPS trackers with SOS button
eSky wireless Inc , https://www.eskygpsiot.com