搜索
bottom↓
回复: 2

请叫马老师一个关于C的小问题

[复制链接]

出0入0汤圆

发表于 2008-2-28 09:35:50 | 显示全部楼层 |阅读模式
马老师您好:
     我现在程序中需要定义一个uchar tab[1024],我是这样定义的
   #pragma data:code
const uchar tab[]
{
  ///里面是128*64点阵
}
#pragma data:data
然后我想改变其中的某些元素,我就定义了一个指针*p,使指针只向数组的第一个元素:
p=tab;
到这里编译的时候出现了一个错误,错误如下
operands of = have illegal types `pointer to unsigned char' and `pointer to const unsigned char'
我用的是ICC,请问马老师:问题怎么解决呢?
谢谢!

阿莫论坛20周年了!感谢大家的支持与爱护!!

曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……

出0入0汤圆

发表于 2008-2-28 10:26:34 | 显示全部楼层
把 p=tab;改成 p=&tab[0];或者 p=&tab试试.

出0入0汤圆

发表于 2008-2-28 14:48:13 | 显示全部楼层
icc的HELP中有说明

The AVR is a Harvard architecture machine, separating program memory from data memory. There are several advantages to such a design. For example, the separate address space allows an AVR device to access more total memory than a conventional architecture. For example, the ATmega series allows up to 64K words of program memory and 64K bytes of data memory, and future devices with possibly even greater amounts of program memory may be available later. However, the program counter still remains at 16 bits.

Unfortunately, C was not invented on such a machine. In particular, C pointers are either data pointers or function pointers, and C rules already specify that you cannot assume data and function pointers can be converted back and forth. However, with a Harvard architecture machine like the AVR, even a data pointer may point to either data memory or to program memory.
There are no standard C rules for how to handle this. The ImageCraft AVR compiler uses the "const" qualifier to signify that an item is in the program memory. Note that for a pointer declaration, the const qualifier may appear in different places, depending whether it is qualifying the pointer variable itself or the items that it points to. For example:

const int table[] = { 1, 2, 3 };
const char *ptr1;
char * const ptr2;
const char * const ptr3;

"table" is a table allocated in the program memory. "ptr1" is an item in the data memory that points to data in the program memory. "ptr2" is an item in the program memory that points to data in the data memory. Finally, "ptr3" is an item in the program memory that points to data in the program memory. In most cases, items such as "table" and "ptr1" are probably the most typical. The C compiler generates the LPM instruction to access the program memory.
Note that the C Standard does not require "const" data to be put in the read-only memory, and in a conventional architecture, this would not matter except for access rights. So, this use of the const qualifier is unconventional, but within the allowable parameters of the C standard. However, this does introduce conflicts with some of the standard C function definitions.

For example, the standard prototype for "strcpy" is strcpy(char *dst, const char *src), with the const qualifier of the second argument signifying that the function does not modify the argument. However, under ICCAVR, the const qualifier would indicate that the second argument points to the program memory which is likely not the case. Thus these functions are defined without the const qualifiers.
Finally, note that only const variables with file storage class will be put into FLASH. For example, variables that are defined outside of a function body or variables that have the static storage class have file storage class. If you declare local variables with the const qualifier, they will not be put into FLASH and undefined behaviors may result. The compiler emits a warning when it detects this situation.

ELPM and RAMPZ
To access Flash memory beyond 64K bytes (e.g. on a M103 or M128), the ELPM instruction must be used in conjunction of setting the RAMPZ bit to 1. A check box ("Use ELPM") has been added to the Compiler Target Options page for this purpose. This is useful for bootloader that are in the high memory. Unfortunately, in the general case, the compiler cannot determine whether a pointer to the flash memory is pointing to the high or low memory. You must enable this check box manually if you are putting constant data in the high memory and you must set the RAMPZ bit yourself.
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-8-25 23:04

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表