打印

Keil C指针心得:

[复制链接]
4031|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
floater|  楼主 | 2007-6-9 22:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
数据类型    [指向存储区]  *[存储区]  指针变量
                   |          |
                   |          |
                   |          |
                   |           ^^^指针变量存储区域
                   |          
                   |          
                   |          
                   |          
                    ^^^^指针变量指向的区域,如:DATA CODE XDATA, 分配2个空间
                    默认是万能的,分配三个字节空间
                    

如:unsigned char xdata *xdata ptr="123"
   
   如果直接引用*ptr是得不到1的值,必须这样引用:
  
   unsigned char xx;
   xx = *((unsigned char code*)ptr);     这样才能引用到1的值

   说明:
   
   如果有指定指向存储区,那么指针变量的值都会指向指定存储区,
   
   unsigned char code chrtest[] = "123";
   unsigned char xdata *ptr;
  
   ptr = chrtest;        这时ptr获取的地址是chrtest在code区分配地址,但是内容的值却                           不是chartest中的内容,而是把得到的地址当做xdata区的地址,当然引用*ptr的数据,就得不到1的值,如果想引用,可以用强制类型转换取得
  
   unsigned char chrtemp;
   
   temp = *((unsigne char code *)ptr);   才能取得:1的数据

相关帖子

沙发
xaccy| | 2007-6-10 01:22 | 只看该作者

正如:

unsigned char xdata prnt_buf[5][22];

uchar xdata *data ptr1;
      
     ptr1=prnt_buf; 
编译后出现如下警告是否需要强制转换才能避免?
warning C182: pointer to different objects

使用特权

评论回复
板凳
ayb_ice| | 2007-6-10 08:08 | 只看该作者

随便说说

规范的写法应该是这样如:unsigned char data * idata p;
指向unsigned char data空间,存储在idata空间。。。

使用特权

评论回复
地板
Airwill| | 2007-6-10 20:51 | 只看该作者

不要误导, 看看 KEIL 的说明

Memory-Specific Pointers
Memory-specific pointers always include a memory type specification in the pointer declaration and always refer to a specific memory area. For example:

char data *str;        /* ptr to string in data */
int xdata *numtab;     /* ptr to int(s) in xdata */
long code *powtab;     /* ptr to long(s) in code */
Because the memory type is specified at compile-time, the memory type byte required by generic pointers is not needed by memory-specific pointers. Memory-specific pointers can be stored using only one byte (idata, data, bdata, and pdata pointers) or two bytes (code and xdata pointers).

Like generic pointers, you may specify the memory area in which a memory-specific pointer is stored. To do so, prefix the pointer declaration with a memory type specifier. For example:

char data * xdata str;         /* ptr in xdata to data char */
int xdata * data numtab;       /* ptr in data to xdata int */
long code * idata powtab;      /* ptr in idata to code long */
Memory-specific pointers may be used to access variables in the declared 8051 memory area only. Memory-specific pointers provide the most efficient method of accessing data objects, but at the cost of reduced flexibility.

The following code and assembly listing shows how pointer values are assigned to memory-specific pointers. Note that the code generated for these pointers is much less involved than the code generated in the generic pointers example listing.

stmt level  source

   1        char data  *c_ptr;      /* memory-specific char ptr */
   2        int  xdata *i_ptr;      /* memory-specific int ptr */
   3        long code  *l_ptr;      /* memory-specific long ptr */
   4
   5        long code powers_of_ten [] =
   6          {
   7          1L,
   8          10L,
   9          100L,
  10          1000L,
  11          10000L,
  12          100000L,
  13          1000000L,
  14          10000000L,
  15          100000000L
  16          };
  17
  18        void main (void)
  19        {
  20   1    char data strbuf [10];
  21   1    int xdata ringbuf [1000];
  22   1
  23   1    c_ptr = &strbuf [0];
  24   1    i_ptr = &ringbuf [0];
  25   1    l_ptr = &powers_of_ten [0];
  26   1    }


ASSEMBLY LISTING OF GENERATED OBJECT CODE

      ; FUNCTION main (BEGIN)
                      ; SOURCE LINE # 18
                      ; SOURCE LINE # 19
                      ; SOURCE LINE # 23
0000 750000 R   MOV   c_ptr,#LOW strbuf
                      ; SOURCE LINE # 24
0003 750000 R   MOV   i_ptr,#HIGH ringbuf
0006 750000 R   MOV   i_ptr+01H,#LOW ringbuf
                      ; SOURCE LINE # 25
0009 750000 R   MOV   l_ptr,#HIGH powers_of_ten
000C 750000 R   MOV   l_ptr+01H,#LOW powers_of_ten
                      ; SOURCE LINE # 26
000F 22            RET
      ; FUNCTION main (END)
 Note

The code generated for a memory-specific pointer executes more quickly than the equivalent code generated for a generic pointer. This is because the memory area is known at compile-time rather than at run-time. The compiler can use this information to optimize memory accesses. If execution speed is a priority, you should use memory-specific pointers instead of generic pointers wherever possible. 
Copyright (c) Keil - An ARM Company. All rights reserved.

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

2

帖子

1

粉丝