pointer loop only iterates once.

I can't get this loop to iterate more than once. All my loops so far have been a breeze. What am I doing wrong? I am thinking it must be the logic? but I don't see it.
1
2
3
4
5
6
7
8
int main()
{
	int array[] = {4,12,8,9,6,12,14,11};
	int array_size = sizeof(array)/sizeof(array[0]);
	for(int *ptr_n = array; ptr_n<array_size + array; ++ptr_n);
	{
			cout<<"loop";
	}
Last edited on
Hi,

There is a minor difference between your code and the code I posted in your last topic about the same thing. It relates to operator precedence.

http://www.cplusplus.com/forum/general/180269/#msg885211


How are you going with the debugger? It should have helped in solving this, by looking at the value of ptr_n.

Try to avoid variable names that match those found in the STL. array is not a good name for a variable. If just fooling around testing things, consider putting My at the beginning of the variable name. Or think about "It is an array of what?" when naming the variable.
You typed a semicolon right after the for loop. If we write your code out a bit with different indent, your loop would become something like this:

1
2
3
4
5
6
for(int* ptr_n = array; ptr_n < array_size + array; ++ptr_n)
    ;

{
    cout << "loop";
}


Your for loop will execute nothing 8 times (the empty semicolon is a null statement, meaning something like "do absolutely nothing"). Then it is followed by a block, which we could write without the brackets as well. So basically it executes nothing 8 times then prints "loop".

Probably you just made a typing mistake and accidentally put the semicolon there, causing your problem. Removing it is enough to make it work.
Alternative way of looping through an array with a pointer.

1
2
3
4
5
6
7
8
9
int numbers[] = {4,12,8,9,6,12,14,11};
int num_items = sizeof(numbers)/sizeof(numbers[0]);
int *ip = numbers; // same as &numbers[0]

while (num_items-- > 0)
{
  cout << *ip << endl;
  ip++;
}
Shadowwolf that nailed it. Thx.

Thomas, I've stashed that algorithm away and filed it. I like changing out the typical forloop.

Hey the Ideas Man, I've delved into the debugger. It is better than the "poor man's" debugger, which will be a hard habit to break. It initially confirmed where the problem was but then subsequently stopped doing it. It will get some getting used to. It is cool to see it display all the relevant pointer and value info. I remember somebody, I don't know who, who said you haven't programmed until you've learned to debug.
Last edited on
@technologist

Well I screwed up with my earlier advice, but if you have cottoned on to using a debugger, then that's awesome.

I guess one eventually develops a sort of self reliance: Being able to fix one's own problems; knowing where to look to find out etcetera. I am sure in production code houses, one is not expecting (or making) a visit to say: "My code doesn't compile, what have I done?". However, there is so much stuff to learn. But if one can take the attitude of nutting out their own problems by always reading the documentation for things they are using, research with Google or Wiki, and try different things, then that puts one well on the way to being a good coder IMO.

Also, I think it is healthy to make some mistakes, as long as one remembers what the cause and fix were. Now that you are aware of the pesky semicolon, you are probably unlikely to make that mistake again. I always put an opening brace after any loop or if statement, by force of habit. These days my editor does the closing brace for me.

There are some (hopefully rare) situations where a null statement is required, and it is good practise IMO to comment it. For example, in K&R C Programming (Ch 5 p88), they have strcpy like this:

1
2
3
4
5
6
/* strcpy copy string t to string s*/
void strcpy(char *s, char *t) 
{ 
   while( *s++ = *t++)   
        ;
} 


I prefer to do this, so one would definitely not miss the null statement:

1
2
3
4
5
6
7
/* strcpy copy string t to string s*/
void strcpy(char* s, char* t) 
{ 
   while( *s++ = *t++)  {  /* always use braces, even for 1 statement */
        ;  /* null statement */
   }
} 


It's quite instructive to see how they started out with this, then boiled it down to this essentially one liner.

Here is a link to K&R, hopefully it won't hurt your c++ skills - it's a completely different way of thinking.

http://zanasi.chem.unisa.it/download/C.pdf
Appreciate !! Thx for the sage advice. I like the autonomous facet, sometimes to the point of me being stubborn (not so great) but I totally get what you are saying.
I am going to keep the post in mind.

Regards,

T
Topic archived. No new replies allowed.