cpointerswhile-loop

iterate through array with while


I'm stuck with the following while loop. EDIT: The goal of the program is to iterate through an array completly from start to end as mentioned in the title. For that I tried to use the while loop

while(*rtn)

is considered false as long as the first value is zero (see example 0 below) so the program doesn't enter the while loop. If I change the first value to be non zero (example 1) the program will enter the while loop. If I change the value assignment so that the fifth element will be zero (example 2) the while loop will stop at the fifth element. The for loop works as expected.

    size_t sz = 100 ;                                                                                                                                                                                                                                                                             
    int *rtn = malloc(sz*sizeof(int));                                                                                                                             
    int cnt ;                                                                                                                                                      
                                                                                                                                                                   
    for(cnt = 0 ; cnt < sz ; cnt++){                                                                                                                               
 EXAMPLE 0 rtn[cnt] = cnt*2 ;        // First value of array is 0                                                                                                                                     
 EXAMPLE 1 rtn[cnt] = cnt*2 + 5 ;    // first value of array is non zero                                                                                                                                     
 EXAMPLE 2 rtn[cnt] = 5 - cnt ;      // fifth value will be 0                                                                                                                                     
    }                                                                                                                                                              
                                                                                                                                                                                                                                                                                                                          
    cnt=0 ;                                                                                                                                                        
    printf("\n---WHILE LOOP---\n") ;                                                                                                                               
    while(*rtn){                                                                                                                                                   
        printf("%3d - %4d\n",cnt, *rtn) ;                                                                                                                          
        cnt++ ;                                                                                                                                                    
        rtn++ ;                                                                                                                                                    
    }                                                                                                                                                              
                                                                                                                                                                   
    printf("\n---FOR LOOP---\n") ;                                                                                                                                 
    for(cnt = 0 ; cnt < 10 ; cnt ++){                                                                                                                              
        printf("%3d - %4d\n", cnt, rtn[cnt]) ;                                                                                                                     
    }                                               
   

I thought the while loop is testing for the NULL pointer not for 0 which obviously can be a valid value!? Also if i change the while loop to

while(rtn){
...
}

it will just go through the complete memory (?) and end with an error. So in essence the while loop is not a valid tool to iterate through an int array?!


Solution

  • Your understanding of the behavior of the while (*rtn) loop in C is correct. The loop condition while (*rtn) evaluates the value pointed to by rtn. If the value is zero, it is considered false and the loop terminates. This is why, in your examples:

    The while (*rtn) loop is indeed checking for the value pointed to by rtn, not for a NULL pointer.

    The condition while (*rtn) is not suitable for iterating through an array if any element of the array can be zero. It stops as soon as it encounters the first zero, which might not be the end of the array.

    To iterate through the entire array, you can use a for loop or a while loop with an explicit counter. Here’s how you can do it using a for loop:

    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
        size_t sz = 100;
        int *rtn = malloc(sz * sizeof(int));
        int cnt;
    
        // Example 0: First value of array is 0
        for (cnt = 0; cnt < sz; cnt++) {
            rtn[cnt] = cnt * 2;
        }
    
        // Example 1: First value of array is non-zero
        for (cnt = 0; cnt < sz; cnt++) {
            rtn[cnt] = cnt * 2 + 5;
        }
    
        // Example 2: Fifth value will be 0
        for (cnt = 0; cnt < sz; cnt++) {
            rtn[cnt] = 5 - cnt;
        }
    
        printf("\n---WHILE LOOP---\n");
        cnt = 0;
        while (cnt < sz) {
            printf("%3d - %4d\n", cnt, rtn[cnt]);
            cnt++;
        }
    
        printf("\n---FOR LOOP---\n");
        for (cnt = 0; cnt < 10; cnt++) {
            printf("%3d - %4d\n", cnt, rtn[cnt]);
        }
    
        free(rtn);
        return 0;
    }
    

    Explanation:

    1. Allocate memory for the array rtn.
    2. Populate the array with values based on your examples.
    3. Use a while loop with an explicit counter to iterate through the entire array.
    4. Use a for loop to print the first 10 elements of the array.

    By using a counter in the while loop (while (cnt < sz)), you ensure that you iterate through the entire array, regardless of the values it contains.