Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 26th, 2006, 03:30 PM
Authorized User
 
Join Date: Apr 2006
Location: Dallas, TX, USA.
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
Default Pointer reference

I have a reference coded like this

(*Ptr).last_name

for an array of struct where last_name is a member of the struct and I want to increment or decrement Ptr to point the another element of the array. What is the correct syntax?

I've tried (*Ptr++) and (*Ptr)++ and (*Ptr-1) and I keep getting syntax errors.

Sam Stamport
__________________
Sam Stamport
Reply With Quote
  #2 (permalink)  
Old April 28th, 2006, 12:40 AM
Authorized User
 
Join Date: Oct 2004
Location: Fayetteville, AR, USA.
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
  #3 (permalink)  
Old April 28th, 2006, 09:04 AM
Authorized User
 
Join Date: Apr 2006
Location: Dallas, TX, USA.
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks for your help.

From what you've said it looks like C++ does not provide a syntax to both increment the pointer and dereference it at the same time when dealing with an array of struct.

However, when I want to both dereference a pointer to a simple array and increment the pointer at the same time the *Ptr++ syntax works just fine.

By the way, is there some setting in this forum where I can get an email notification when a reply is posted to my questions?

Sam Stamport
Reply With Quote
  #4 (permalink)  
Old April 29th, 2006, 07:16 AM
Authorized User
 
Join Date: Oct 2004
Location: Fayetteville, AR, USA.
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to C@uark
Default

If you wanted to increment the array index to next contiguous element and then the dereference the pointer in the array in one fail swoop. This might work don’t know for sure, I don’t have a complier on me, but the order of it seems logical.

If Ptr is the array variable name (which by default contains the address of the first element) given to your array of structs.

(*(Ptr++)).last_name // should evaluate address of pointer array increment it to the next contiguous element, then dereference the pointer stored inside with '*' and select the member last_name with dot operator. If thats more along the lines of what your referring to.
Reply With Quote
  #5 (permalink)  
Old April 29th, 2006, 07:44 AM
Authorized User
 
Join Date: Oct 2004
Location: Fayetteville, AR, USA.
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to C@uark
Default

This should also do the same thing:

(Ptr++)->last_name
Reply With Quote
  #6 (permalink)  
Old April 29th, 2006, 08:51 AM
Authorized User
 
Join Date: Apr 2006
Location: Dallas, TX, USA.
Posts: 17
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks so much for your help.

Only one more week to go in the semester then the final exam. I can't wait 'till it's over!

Sam Stamport
Reply With Quote


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to struct goscho C++ Programming 1 January 10th, 2008 07:23 AM
Constant pointer and pointer to a constant amit_mande@yahoo.com C++ Programming 2 June 5th, 2007 01:39 AM
Pointer fReqZz C++ Programming 5 June 5th, 2007 01:09 AM
NULL Pointer kumar_raj13 C++ Programming 0 July 15th, 2005 01:10 AM
pointer-to-function in C++ jacob C++ Programming 2 October 23rd, 2004 05:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.