-1

I declared and initialized an array in main method of C program and defined a method to print the array elements, but it prints some garbage values instead of the actual value.

I tried the following code.

#include <stdio.h>

void arraySize(int arr[]);

int main() {
    int array[] = {15, 50, 34, 20, 10, 79, 100};

    arraySize(array);

    return 0;
}

void arraySize(int arr[]) {
    int i = 0;

    while(arr[i]) {
        printf("The value of arr[%d] is %d \n", i, arr[i]);
        i++;
    }
}

I'm getting the following output

The value of arr[1] is 50
The value of arr[2] is 34
The value of arr[3] is 20
The value of arr[4] is 10
The value of arr[5] is 79
The value of arr[6] is 100
The value of arr[7] is 3538944
The value of arr[8] is 28
The value of arr[9] is 6422300
The value of arr[10] is 3538944
The value of arr[11] is 6422368
The value of arr[12] is 4198966
The value of arr[13] is 1
The value of arr[14] is 8263032
The value of arr[15] is 8266360
The value of arr[16] is 2
The value of arr[17] is 0

Kindly assist me how to print the array values using while loop and terminate the loop once end of record reached.

B.Balamanigandan
  • 4,713
  • 11
  • 68
  • 130
  • You are reading beyond the end of the array. This results in "undefined behaviour" (google that term). – Jabberwocky Nov 19 '19 at 10:22
  • 1
    Arrays don't magically end with a NULL or 0. You need to either explicitly add such a value to the end of the array or pass in the array size into the function. – kaylum Nov 19 '19 at 10:23
  • How will the compiler know about the size of the `array` passed? Pass it separately using `sizeof` & then use it. – Gaurav Nov 19 '19 at 10:25
  • @kaylum - Okay but for the index 1 to 6, it needs to give the proper value, why its not printed the correct value? could you please tell. – B.Balamanigandan Nov 19 '19 at 10:26
  • @B.Balamanigandan what is the _correct value_? – Jabberwocky Nov 19 '19 at 10:26
  • @Jabberwocky - {15, 50, 34, 20, 10, 79, 100} – B.Balamanigandan Nov 19 '19 at 10:27
  • @B.Balamanigandan but it does print the correct values for indexes 0 to 5. All other values are read from beyond the array. Reading beyond the end of an array is undefined behaviour which in this case results in getting seemingly random values. – Jabberwocky Nov 19 '19 at 10:28
  • It would seem you conveniently left out the first line of output in your paste. – WhozCraig Nov 19 '19 at 10:29
  • 2
    @B.Balamanigandan You changed the code. [Previously](https://stackoverflow.com/revisions/58931686/2) you had `i++` before the `printf`. Which would result in the first element in the array not getting printed exactly as you have shown. I suspect you did not update the output after changing the code. – kaylum Nov 19 '19 at 10:30
  • 1
    @B.Balamanigandan your output doesn't match the program. Please read this before posting your next question here: [ask]. – Jabberwocky Nov 19 '19 at 10:31
  • @B.Balamanigandan [This SO article](https://stackoverflow.com/questions/492384/how-to-find-the-sizeof-a-pointer-pointing-to-an-array) may be interesting for you. – Jabberwocky Nov 19 '19 at 10:37

3 Answers3

1

This doesn't work because it can't just find out the array size like that. int arr[] decays into a pointer and your function has no way of knowing the array's size. It will just keep on running after reaching the end of the array, which is undefined behavior. What likely happens is that it prints random data on the stack until it encounters something that represents a 0 as an integer, or until there's an access violation.

while(arr[i]) doesn't do what you seem to think it does. It doesn't check whether that's valid, it just checks whether arr[i] has the value 0 (which then is implicitly converted to false). Coincidentally, this means that you can use 0 as a sentinel value to signal that you reached the end of the array:

int array[] = {15, 50, 34, 20, 10, 79, 100, 0};

This will now stop printing when the 0 is reached and you don't have undefined behavior anymore.

Blaze
  • 16,736
  • 2
  • 25
  • 44
  • Okay, I accept it. but why its not printed for the index from 1 to 6, as per your said it needs to print the {15, 50, 34, 20, 10, 79, 100} and once it goes beyond the array size, it may print some garbage value. – B.Balamanigandan Nov 19 '19 at 10:28
  • @B.Balamanigandan the first array index is 0 not 1. Is that what you're wondering about? – Jabberwocky Nov 19 '19 at 10:30
  • 1
    @B.Balamanigandan once it goes past the array's limit it's undefined behavior so there's no guarantee on what happens. Printing garbage values is a common manifestation of UB when trying to print values, but it could also do something entirely different. – Blaze Nov 19 '19 at 10:31
  • @B.Balamanigandan You changed the code. [Previously](https://stackoverflow.com/revisions/58931686/2) you had `i++` before the `printf`. Which would result in the first element in the array not getting printed exactly as you have shown. I suspect you did not update the output after changing the code. – kaylum Nov 19 '19 at 10:33
1

The array does not contain an element with the value equal to 0. So the condition of the loop

while(arr[i])

that is equivalent to

while( arr[i] != 0 )

invokes undefined behavior.

You have to pass to the function also the number of elements you are going to output. for example

void arraySize( const int arr[], size_t n);

and

void arraySize( const int arr[], size_t n )
{
    for ( size_t i = 0; i < n; i++ )
    {
        printf("The value of arr[%zu] is %d \n", i, arr[i]);
    }
}

And in main write for example

int array[] = {15, 50, 34, 20, 10, 79, 100};
const size_t N = sizeof( array ) / sizeof( *array );

arraySize( array, N );

Bear in mind that you can not calculate the size of the passed array within the function. Th compiler implicitly adjust the parameter of the array type to pointer to the element type.

That is these two function declarations are equivalent and declare the same one function

void arraySize( int arr[] );
void arraySize( int *arr );

Pay attention to that the name arraySize of the function that outputs elements of an array only confuses readers of the code. You could name it as for example printArray.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

Change it to:

#include <stdio.h>

void arraySize(int arr[], int);

int main() 
{
    int array[] = {15, 50, 34, 20, 10, 79, 100};

    arraySize(array, sizeof(array)/sizeof(array[0]));  //adding size of array as new argument

    return 0;
}

void arraySize(int arr[], int siz) 
{
    int i = 0;

    while(i<siz) 
    {
        printf("The value of arr[%d] is %d \n", i, arr[i]);
        i++;
    }
}

Compiler doesn't know about the size of the array passed on its own, so to set the limit you'll have to explicitly send the size.

Gaurav
  • 1,570
  • 5
  • 17