JimS
JimS

Reputation: 349

iterate through array elements using its own pointer

int* arr = new int[30](); //30 is random serving the example's purposes

while(<insert check here that uses the arr pointer>){

   cout<<*arr<<endl;

   arr++;
}

This is me experimenting on tricks using pointers and I have been working on this for some time today. I have used many different checks in the while clause without ever getting it right (mostly ending up with infinite loops or seg faults). So my question can this be done or the standard for-loop method is the only way?

Upvotes: 0

Views: 1988

Answers (1)

joeking
joeking

Reputation: 2096

Well, there are many ways to do this.

add

int *end = arr + 30;
while (arr < end)

Of course, by incrementing "arr", you are losing the pointer to the array - but you knew that already :-)

Upvotes: 1

Related Questions