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 CCS Technical Support

Using List and pointer and typedef

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



Joined: 31 Jan 2009
Posts: 59

View user's profile Send private message

Using List and pointer and typedef
PostPosted: Fri Jun 17, 2011 8:28 am     Reply with quote

hi all

I want to have a list with both forward and backward.
so i defined this:
Code:

typedef struct tnode {
  struct tnode *previos;
  int16 value;
  struct tnode *next;
} myNode;
myNode memory;


Thing is when I add item I use:
Code:

myNode newNode;
newNode.value = 100;

newNode.previos = memory;
memory.next = newNode;


Is this right ?
I tried to use newNode->prev but it fails compilation with :
Previous identifier must be a pointer

ccsc compiler 4.120
pic 18f8527
jeremiah



Joined: 20 Jul 2010
Posts: 1349

View user's profile Send private message

PostPosted: Tue Jun 21, 2011 7:21 am     Reply with quote

The arrow operator ("=>") and the dot operator (".") are picked dependent on whether the structure itself is a pointer or not. The structure elements themselves do no dictate that.

Lets take your typedef for an example and add a little:

Code:

typedef struct tnode {
  struct tnode *previous;
  int16 value;
  struct tnode *next;
} myNode;

myNode memory;
myNode *memory_ptr;

//make sure to allocate memory for the pointer version:
memory_ptr = someMemoryAllocationFunction();

//now initialize each:
memory.previous = NULL:
memory.value = 0;
memory.next = NULL:

memory_ptr->previous = NULL;
memory_ptr->value = 0;
memory_ptr->next = NULL;



The use of the dot versus the arrow operator is purely based on the type of the structure object itself, but not it's members.

For your bottom example to work, you would need to do the following:


Code:

myNode newNode;
newNode.value = 100;

newNode.previos = &memory;
memory.next = &newNode;


The & sign at the front of a variable name gives the address of the object, which is what is compatible with a pointer.

HOWEVER, be very careful doing this. If you assign a pointer to an object like this, you need to make sure the object is always available to the pointer. If you do this in a function, the moment you leave the function, the pointer will be invalid, and the results of using that pointer are not defined.
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