C18 compiler. struct issues.

Status
Not open for further replies.

HerbertMunch

New Member
Im trying to make a member of this struct reference another instance of the same struct.

Code:
typedef struct MenuItem
{
	
	//Pointer to the an array of menuitems that represents the next level of heirarchy.
	MenuItem *nextLevel;
	
	

}MenuItem;

The compiler is not happy.
Can this not be done in c?
 
Would it be okay to do this instead, and then cast later?

Code:
typedef struct MenuItem
{
	
	//Pointer to the an array of menuitems that represents the next level of heirarchy.
	void *nextLevel;
	
	

}MenuItem;

Thanks
 
Would it be,
Code:
typedef struct MenuItem
{
	
	//Pointer to the an array of menuitems that represents the next level of heirarchy.
	near rom * MenuItem;

}MenuItem;

Mike.
 
BTW, I added a message print routine to your code that uses a rom pointer.

Code:
void PutMessage(char rom*);
void PutMessageAt(unsigned char,unsigned char,char rom*);


void main(void){
    InitLCD();
    PutMessage((rom char*) "Hello World!");
    PutMessageAt(3,2,(rom char*) "3 in on Line 2");
    while(1);
}

//Write a string to the LCD
void PutMessage(char rom *Message){
	while(*Message!=0)
	    WriteChar(*Message++);
}

//Set position and write string.
void PutMessageAt(unsigned char X,unsigned char Y,char rom *Message){
    WriteCommand(0x80+(--Y<<6)+--X);
	while(*Message!=0)
	    WriteChar(*Message++);
}

Mike.
 

Nicely done Mike. A wonderful example using pointers which has thus far eluded and confounded me.
 
Pointers on the pic are confusing. The default is that pointers point to ram. I'm slowly getting comfortable with the types of pic pointers and have just realised that the cast on the function call is now not needed, this is from when I was trying everything to get it working.

So,
Code:
        PutMessage("Hello World!");

Should work fine.

Mike.
 

ok thanks very much mate.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…