Thread: Condition of the for loop with defined arraylength(*array)

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    54

    Condition of the for loop with defined arraylength(*array)

    Hi!

    EDIT: this was an irrelevant post - the array size is specified in the program I am referring to, so no magic. I can't delete the post, so I am sorry if you have lost your time reading it.

    Please, help me to understand how the size of the array is used in the following part of the program, which I have found on this forum.
    We define the size of the array as
    Code:
    #define ARRAYLENGTH(x) (sizeof(x)/sizeof(*(x)))
    Then in the program we get information from the file and put that into the array using the for loop. My question about the condition of the loop
    Code:
    j < ARRAYSIZE(*array)
    : when the loop starts, the array is not yet populated, so the size is equal to sizeof(the whole array which is null at the first iteration of the loop)/sizeof(first member of the array)). Please, help me to understand how this condition works, and how to interpret it.


    Code:
          for ( i = 0; fgets(buffer, sizeof buffer, file); ++i )
          {
             for ( j = 0, ptr = buffer; j < ARRAYLENGTH(*array); ++j, ++ptr )
             {
                array[i][j] = (int)strtol(ptr, &ptr, 10);
             }
          }
    

    Thanks a lot!
    Last edited by Ducol; 12-12-2016 at 08:50 AM. Reason: additional information

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You haven't told us what "array" is. It could be anything. And the fact that it's being dereferenced in the ARRAYLENGTH call is suspicious. That's not usually how that code is used.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. do-while loop with condition
    By xenocid3r in forum C Programming
    Replies: 1
    Last Post: 03-02-2013, 12:20 PM
  2. While loop not skipping when condition isn't met
    By MNM1245 in forum C Programming
    Replies: 7
    Last Post: 11-04-2012, 05:32 PM
  3. what condition in the while loop?
    By joeman in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2010, 11:49 AM
  4. need while(for(loop)) to be condition
    By thestien in forum C++ Programming
    Replies: 4
    Last Post: 10-12-2006, 08:32 AM
  5. help with loop condition
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 03-23-2002, 12:18 PM

Tags for this Thread