Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Pointer arithmetic

Reply
Thread Tools

Pointer arithmetic

 
 
dan
Guest
Posts: n/a
 
      01-06-2004
Hello,
An Iterator class declares a data member that points to the current
index of an array within a container class. It also implements a
member function that determines whether or not the data member is
pointing to the last index in the array using pointer arithmetic.

class CIterator
{
public:
CIterator(CContainer& c) : myContainer,
mypData(myContainer.mypArray) {}

void Reset() { mypData = myContainer.mypArray; }
bool IsEnd()
{
return ((mypData - myContainer.mypArray) >=
myContainer.mySize);
}

private:
CContainer& myContainer;
int *mypData;
};

My question is in regards to the way c++ points to memory. If {
mypData = myContainer.mypArray; } assigns the address of the first
element in the array to mypData, then how is it that ((mypData -
myContainer.mypArray) >= myContainer.mySize); the first operand which
is basically an address in memory, (correct?) can relate to the second
operand (mySize) which is an int with a relational operator? the 2
data types don't seem compatible.

Daniel
 
Reply With Quote
 
 
 
 
Jeff Schwab
Guest
Posts: n/a
 
      01-06-2004
dan wrote:
> Hello,
> An Iterator class declares a data member that points to the current
> index of an array within a container class. It also implements a
> member function that determines whether or not the data member is
> pointing to the last index in the array using pointer arithmetic.
>
> class CIterator
> {
> public:
> CIterator(CContainer& c) : myContainer,
> mypData(myContainer.mypArray) {}
>
> void Reset() { mypData = myContainer.mypArray; }
> bool IsEnd()
> {
> return ((mypData - myContainer.mypArray) >=
> myContainer.mySize);
> }
>
> private:
> CContainer& myContainer;
> int *mypData;
> };
>
> My question is in regards to the way c++ points to memory. If {
> mypData = myContainer.mypArray; } assigns the address of the first
> element in the array to mypData, then how is it that ((mypData -
> myContainer.mypArray) >= myContainer.mySize); the first operand which
> is basically an address in memory, (correct?)


No, it's difference between two addresses. It's usually a signed
integer type called std:trdiff_t (defined in <cstddef>).

> can relate to the second
> operand (mySize) which is an int with a relational operator? the 2
> data types don't seem compatible.
>
> Daniel


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Usual Arithmetic Conversions-arithmetic expressions joshc C Programming 5 03-31-2005 02:23 AM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57