True Beginner - Pointer/Array For loops

This is a discussion on True Beginner - Pointer/Array For loops within the C Programming forums, part of the General Programming Boards category; Hello all, This is my first post and I am a complete novice at C programming. I am trying to ...

  1. #1
    Registered User KyussRyn's Avatar
    Join Date
    Mar 2009
    Location
    Tokyo, Japan
    Posts
    7

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    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
    Last edited by laserlight; 03-04-2009 at 02:20 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User KyussRyn's Avatar
    Join Date
    Mar 2009
    Location
    Tokyo, Japan
    Posts
    7

    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++);
    Thank you for all answers.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by KyussRyn View Post
    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++);
    Thank you for all answers.
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    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. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    pls help me .. programming was so difficult..

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    hi please help me with this cprogramming

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    11

    Smile

    hi.. you are also asking some help huh.. hehehehe
    me too.. programming was so difficult..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Applied philosophy refresher
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 04-28-2009, 09:55 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Looking for feedback on program segment
    By avron in forum C++ Programming
    Replies: 4
    Last Post: 05-07-2007, 04:38 PM
  4. MCI CD Player
    By soutine in forum Windows Programming
    Replies: 0
    Last Post: 11-02-2001, 04:03 PM
  5. True or False Quiz (Need help)
    By Twiggy in forum C Programming
    Replies: 9
    Last Post: 10-12-2001, 04:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21