View previous topic :: View next topic |
Author |
Message |
object01
Joined: 13 May 2004 Posts: 90 Location: Nashville, TN
|
Structure pointer syntax question |
Posted: Mon Oct 25, 2004 12:20 pm |
|
|
I'm trying to understand why one of the statements in the following code works, but the other doesn't. Am I missing some subtlety of the syntax?
Code: | #include <18F2320.h>
struct STRUCTDEF {
unsigned int8 field1;
};
struct STRUCTDEF * __STACK[8];
unsigned int8 __STACKTOP = 0;
/*
struct STRUCT_ATINFO * getStackTop() {
return __STACK[__STACKTOP];
}
*/
#define getStackTop() (__STACK[__STACKTOP])
void main() {
struct STRUCTDEF s;
struct STRUCTDEF *sPtr;
unsigned int8 value;
s.field1 = 42;
__STACK[__STACKTOP] = &s;
// If this works,
sPtr = getStackTop();
value = sPtr->field1;
// then why doesn't this work?
value = (getStackTop())->field1;
} |
--
Jeff S. |
|
|
object01
Joined: 13 May 2004 Posts: 90 Location: Nashville, TN
|
|
Posted: Mon Oct 25, 2004 12:24 pm |
|
|
Casting the macro result like so,
Code: | #define getStackTop() ((struct STRUCTDEF *)__STACK[__STACKTOP]) |
seems to solve the problem, but I don't understand why. Can't the compiler infer the type with the array name alone?
--
Jeff S. |
|
|
object01
Joined: 13 May 2004 Posts: 90 Location: Nashville, TN
|
|
Posted: Mon Oct 25, 2004 1:03 pm |
|
|
Another curiosity. Why doesn't using a typedef for the array type and function return type work?
--
Jeff S.
Code: | #include <18F2320.h>
struct STRUCTDEF {
unsigned int8 field1;
};
typedef struct STRUCTDEF *STRUCTPTR;
STRUCTPTR __STACK[8];
unsigned int8 __STACKTOP = 0;
STRUCTPTR getStackTop() {
return __STACK[__STACKTOP];
}
//#define getStackTop() ((struct STRUCTDEF *)__STACK[__STACKTOP])
void main() {
struct STRUCTDEF s;
struct STRUCTDEF *sPtr;
unsigned int8 value;
s.field1 = 42;
__STACK[0] = &s;
// If this works,
sPtr = getStackTop();
value = sPtr->field1;
// then why doesn't this work?
value = getStackTop()->field1;
}
|
|
|
|
|