jinbu 发表于 2008-2-28 09:35:50

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

马老师您好:
   我现在程序中需要定义一个uchar tab,我是这样定义的
   #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,请问马老师:问题怎么解决呢?
谢谢!

guaizima 发表于 2008-2-28 10:26:34

把 p=tab;改成 p=&tab;或者 p=&tab试试.

machao 发表于 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.
页: [1]
查看完整版本: 请叫马老师一个关于C的小问题