# Thread: True Beginner - Pointer/Array For loops

1. ## True Beginner - Pointer/Array For loops

Hello all,

This is my first post and I am a complete novice at C programming. I am trying to teach myself C programming and using "Programming in C" by Stephen G. Kochan 3rd Ed. My problem is that I have a pointer that points to an array and I want it to do (what I assumed) was a simple 'for' loop to cycle through the elements of the array using this code:
Code:
```int main(void)
{
int array[3]={5,15,7};
int *arrPntr;

for (arrPntr = array; arrPntr<(arrPntr+2); arrPntr++)
printf("%i\n", *arrPntr);
getchar();
}```
but it loops through the code and doesn't stop (an infinite loop until it crashes).

I have found a work around in :
Code:
`    for (arrPntr = array; arrPntr<&array[3]; arrPntr++)`
but in my book, it says that the first example should work (for anyone who has the book it is Chap 11, page 262: Paraphrased "'valuesPtr > &values[99]' is the same as ' valuesPtr > values + 99'".

Could someone please tell me if:
1. I have made a mistake with my code (which I think is the case)
2. The text is incorrect or outdated, and if so please provide me with up to date information.

Thank you for any assistance you can offer.

2. The problem is that you have set a moving target that you can never hit: mathematically, arrPntr<(arrPntr+2) is always true, hence you have an infinite loop.

What your book probably was getting at is: arrPntr < array + 3. Now, array does not change since it is, uh, an array. It gets converted to a pointer to its first element, and then 3 is added to it, so the resulting pointer points one past the last element of the array. Hence, when arrPntr is incremented past the last element of the array, arrPntr < array + 3 no longer holds true, so the loop terminates.

Now, &array[3] is indeed the same as array + 3, except that conceptually it involves accessing array[3], which normally leads to undefined behaviour since there is no such element. If I remember correctly, there is no undefined behaviour in this special case because the C standard defines the net effect to be array + 3. I would just write array + 3 to avoid possible misunderstanding, and besides, it is easier to write

3. ## I am an idiot

Laserlight, thank you and you are right. I just read the book incorrectly. Thank you for your prompt response to my question to much appreciated.

One small addition to this is in relation to convention, is this loop style recommended or should I use:
Code:
`for(int i=0; i<3; i++);`

4. Originally Posted by KyussRyn
Laserlight, thank you and you are right. I just read the book incorrectly. Thank you for your prompt response to my question to much appreciated.

One small addition to this is in relation to convention, is this loop style recommended or should I use:
Code:
`for(int i=0; i<3; i++);`
There is no "right" and "wrong" here - it's about style and purpose. So, it would depend on what makes most sense in the situation. You may for example want to achieve that you have a pointer to an element within the array (a search for value x), in which case iterating over the pointer will be a better idea. If, on the other hand, you are simply trying to print the values in an array, I personally prefer to use a simple integer loop counter (particularly if the number of iterations is known/easily calculated).

--
Mats

5. hi sir can you please do this program for me..? i want only the code of this problem.. so here is the problem, "write a program that will compute for and display the sum of all numbers divisible by 3 from 1-1000." ill wait for your reply.. thank you...

from JANE

6. pls help me .. programming was so difficult..