CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Structure pointer syntax question

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
object01



Joined: 13 May 2004
Posts: 90
Location: Nashville, TN

View user's profile Send private message Send e-mail Visit poster's website

Structure pointer syntax question
PostPosted: Mon Oct 25, 2004 12:20 pm     Reply with quote

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

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Mon Oct 25, 2004 12:24 pm     Reply with quote

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

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Mon Oct 25, 2004 1:03 pm     Reply with quote

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;
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group