473,394 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Pointer arithmetic

dan
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
Jul 22 '05 #1
1 1437
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::ptrdiff_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


Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: ceo | last post by:
Hi, Following is a program that doesn't give the expected output, not sure what's wrong here. I'm adding the size of derived class to the base class pointer to access the next element in the...
22
by: Alex Fraser | last post by:
From searching Google Groups, I understand that void pointer arithmetic is a constraint violation, which is understandable. However, generic functions like qsort() and bsearch() must in essence do...
6
by: Francois Grieu | last post by:
Are these programs correct ? #include <stdio.h> unsigned char a = {1,2}; int main(void) { unsigned char j; for(j=1; j<=2; ++j) printf("%u\n", *( a+j-1 )); return 0; }
3
by: randomtalk | last post by:
hello everyone! Well, recently i've been trying to pick up c and see what is pointer all about (been programming in lisp/python for the better part of my last two years).. mmm.. I'm currently...
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
7
by: barikat | last post by:
int a; int *Ptr1, *Ptr2; Ptr1 = a; Ptr1++; Ptr2 = a; printf("Ptr1 : %p\n", Ptr1); printf("Ptr2 : %p\n\n", Ptr2);
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
41
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what...
19
by: =?iso-8859-1?b?VG9t4XMg0yBoyWlsaWRoZQ==?= | last post by:
Coming originally from C++, I used to do the likes of the following, using a pointer in a conditional: void Func(int *p) { if (p) { *p++ = 7; *p++ = 8;
25
by: Ioannis Vranos | last post by:
Are the following codes guaranteed to work always? 1. #include <iostream> inline void some_func(int *p, const std::size_t SIZE) {
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.