View previous topic :: View next topic |
Author |
Message |
LiamLiam
Joined: 21 Sep 2008 Posts: 7
|
typedef const char hello; << why is this invalid |
Posted: Sun Sep 28, 2008 4:35 am |
|
|
Hi All
Currently trying to figure out why the following is invalid:
Code: | typedef const char hello; |
when Code: | typedef unsigned int8 hello; | works fine.
Running 3.249 PCH
Any ideas?
Many thanks |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
Re: typedef const char hello; << why is this invalid |
Posted: Sun Sep 28, 2008 9:46 am |
|
|
typedef defines an alternate name for an existing data type. In your example, "unsigned int8" is an existing data type. But "const" is not a data type. "const" is a storage class, like "static" and "extern", and so cannot be part of a typedef.
You can do this:
Code: |
typedef char hello;
. . .
const hello x;
|
_________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
LiamLiam
Joined: 21 Sep 2008 Posts: 7
|
|
Posted: Sun Sep 28, 2008 10:40 am |
|
|
Hi RLScott
The thing is that I have purchased a LCD driver / display library written in ANSI C.
As a way of abstracting itself from the specific data types of the compiler, they have use typedef. An example is:
Code: | typedef GCONSTP char PGENERIC * PGCSTR; /* Pointer to constant string (may be in ROM or RAM) */ |
They have used this syntax quite liberally, so to change it would be a real pain. Am I missing something? |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
|
Posted: Sun Sep 28, 2008 10:54 am |
|
|
LiamLiam wrote: |
Code: | typedef GCONSTP char PGENERIC * PGCSTR; /* Pointer to constant string (may be in ROM or RAM) */ |
|
If GCONSTP is defined as a string that contains "const", then this is not ANSII C.
Edit: After further review, it appears that "const" is not actually a storage class, but is a qualifier borrowed from C++, so it may or may not be allowed, depending on the way a particular compiler treats it. But it is certainly not a data type. _________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sun Sep 28, 2008 11:40 am |
|
|
A pointer to ROM data is effectively a separate data type with many compilers, but it isn't a portable construct.
Apart from the said syntax problem, you should be aware that the handling of ROM constants and particularly ROM pointers may be incompatible with CCS C's way of treating this stuff. You probably saw only the tip of the iceberg. |
|
|
Ttelmah Guest
|
|
Posted: Sun Sep 28, 2008 2:35 pm |
|
|
Worth understanding, that in ANSI, a 'const', is _not_ a ROM data definition. In ANSI, all 'const' implies is that the target data, is read-only, not read-write. This is really not implementable, in hardware that doesn't have memory manager hardware to stop writing to areas where it is not allowed. All CCS does for 'const', in ANSI mode, is to copy the ROM data to RAM, just like a normal variable, and block normal write operations. This though won't work for pointer based operations.
You might as well remove the 'const' from the declarations....
As a separate comment though, remember that #define can be used to give similar behaviour to typedef, for operations like this.
Best Wishes |
|
|
LiamLiam
Joined: 21 Sep 2008 Posts: 7
|
|
Posted: Tue Sep 30, 2008 5:12 am |
|
|
Gents, thanks for your input and replies.
I have got the low level stuff working now (writing to screen etc), so the drivers have been of some [although little] value.
I will attempt to get the rest working, but as FvM pointed out there will undoubtedly be many more problems. It will be good for a challenge at least.
For anybody who is interested, these are the RAMTEX drivers. |
|
|
|