Hi,
I am a bit confused about writing codes with a poitner pointed to a huge table in C with Keil compiler (for C166 MCU).
First if i want to define a pointer pointed to a huge table, naturally i would do:
int huge (*ptable)[256];
However the complier does not allow to write in this way. The only way to pass the compiling is:
int (huge *ptable)[256];
The codes seem to work like this. But i am not really sure if i defined the pointer in a correct way, Can someone confirm me about it?
Second question about type cast. If i want to convert something into a pointer, I found i can do both type casts:
long addr; int (huge *ptable)[256]; ptable = (int huge (*)[256])(&addr); ptable = (int (huge *)[256])(&addr);
Both of the 2 lines above seem to work. But i am confused why the complier allows to define a point only in 1 way but allows type cast in 2 different ways? Does anyone knows the story under the hood?
Thanks for the help.
Regards, Xiang
...naturally i would do...
This seems natural to you, but it's nowhere near natural to me. You should declare pointers to arrays in the same way everyone does: as pointers to a single element of the array:
int huge *ptable;
The K&R book has a very good discussion about arrays and pointers. You should read it.
Regards,
- mike
ok, by saying "naturally" i meant my understanding to the keyword "huge", not about how to define a pointer. Because the (*ptable) is already a pointer, defining in the way like "int huge (*ptable)[x]" is more logicall to me, compare to "int (huge *ptable)[x]".
The thread is asking about how to use the huge keyword in combine with defining and type casting a pointer to an array type variables. Now about how should someone use a pointer.
And i do really want a pointer to an exact size of array, such that the compiler can make a double check, when the memory size is not correctly allocated.
your way
define only a pointer to an int type, which can ofc work with array, but it is not what i want.
Sorry, I didn't realize that one could declare a pointer to an array with specified size. Is this a feature introduced in C99? Note that C166 does not claim C99 compliance. The behavior of the compiler does look odd. As for verifying if the declared pointer is really a pointer to huge int, you can look at disassembly of data accesses with this pointer. Besides, huge is a compiler-specific language extension. It doesn't necessarily behave the same way the normal type qualifiers do. Which is a shame, really.
Is this a feature introduced in C99?
I know keil c166 compiler only supports C90 but not C99. But i don't have those standards (no time to study and no money to buy), although i'd really like to spend some time to understand the C standrads.
From the draft of C99 from this link: www.open-std.org/.../n1256.pdf It mentioned the use of pointer to a table, like in page 122:
3 EXAMPLE: (d) int (*)[3] (d) pointer to an array of three ints
I cannot find C90 texts anywhere. But the Keil compiler does support this feature. So i guess it is already included in C90.
Normally i prefer to use a pointer to a table when there is a need to pass a table as a function parameter. This reminds me and other people to be careful to use correct memory objects when calling sth like:
extern void func(int (*conversion_table)[10]); int table_to_send[10]; func(&table_to_send);
In fact the Keil compiler doesn't check the table size, it passes the compiling if doing:
extern void func(int (*conversion_table)[10]); int table_to_send[9]; func(&table_to_send);
So the way I am doing is more like just a note for myself to send right table when calling a function.
About the huge (and far/xhuge) keyword, I just checked with assembly codes that it works in this way to define a pointer to a huge table:
int (huge *ptable)[10];
View all questions in Keil forum