jeremiah
Joined: 20 Jul 2010 Posts: 1349
|
|
Posted: Tue Jun 21, 2011 7:21 am |
|
|
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. |
|