Summary of C51 pointer definition and application

Publisher:XiangtanLatest update time:2011-09-27 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
1. Definition of pointer variables
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.

  1. 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.
  2. 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.
  3. 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;
  4. 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

  1. int x, j;
    int * px, *py;
    px=&x; py=&y;
  2. *px=0; py=px;
  3. *px++<=>*(px++)
  4. (*px)++<=>x++
  5. unsigned char xdata * x;
    unsinged char xdata * y;
    x=0x0456;
    *x=0x34 //Equivalent to mov dptr,#456h; mov a,#34h; movx @dptr,a
  6. unsigned char pdata * x;
    x=0x045;
    *x=0x34 //equivalent to mov r0,#45h; mov a,#34h; movx @r0,a
  7. unsigned char data * x;
    x=0x30;
    *x=0x34 //equivalent to mov a,#34h; mov 30h ,a
  8. 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.
  9. 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.
  10. px=*((int xdata * xdata *)0x4000); //How to analyze?
  11. 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.
  12. 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

  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]…..
    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
  2. char * s1
    char code str[]=”abcdefg”
    s1=str;
  3. char *s1=”abcdefg”;

4. Pointers and structures

  1. 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.
  2. struct student
    {
    char name[20];
    int num;
    }stu1,stu2;
  3. 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;
  4. struct student stu[10];
    struct student * p;
    p=stu;
Reference address:Summary of C51 pointer definition and application

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

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号