View Single Post
  #2 (permalink)  
Old April 28th, 2006, 12:40 AM
C@uark C@uark is offline
Authorized User
 
Join Date: Oct 2004
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to C@uark
Default

(*Ptr).last_name is the same as this Ptr->last_name if Ptr is the struct variable name.
the "*" derferences the variable name Ptr the "."(direct access operator) selects the member of the struct.
the "->" (indirect access operator) does exactly the same thing but is less confusing.

if you have an array of these struct pointer. i.e

struct your_type* ptrArray[10]; // an array of 10 pointers of your_type

to iterate through this would be like any other array

for(int i=0; i<10; i++)
{
    cout<<ptrArray[i]->last_name; // index into ptr array derefencing last_name with indirect operator
}
or like this
for(int i=0; i<10; ptrArray++) // iteratates by incrementing ptrArray adrress by 1 untill i >= 10
{
     cout<<ptrArray->last_name;
}
you can like wise declare an array of pointers with a pointer to allocate dynamically
struct your_type** ptrArray;

ptrArray = new your_type*[10]; // dynamically allocated array of 10 pointers of your_type

interating through this array is identical to the other example.


Now if your structs where not dynamically allocated you should of had an array declared as followed

struct your_type myArray[10]; // an array of 10 structs of your_type

iterating would be identical however derefencing each struct in the array would not. i.e.
for(int i=0; i<10; i++)
{
     cout<<myArray[i].last_name // derefence with direct access operator
}

for(int i=0;i<10;myArray++)// iterates by incrementing address of myArray by 1
{
cout<<myArray.last_name; // dereference last_name with dot operator
}
Reply With Quote