0

I was trying to a simple program of passing the inputted numbers of the arrays and displaying it using the function but it shows random value. What am I doing wrong?

#include<stdio.h>
#define size 5

void display();
int main()
{
    int arr[size];
    int i;

    for(i = 0; i < size; i++){
        printf("Input a number at index %d: ", i);
        scanf("%d", &arr[i]);
        
    }
    display();
}

void display(){
    int i;
    int arr[size];
     for(i = 0; i < 5; i++)
        printf("Element[%d] = %d\n", i, arr[i]);
}
Gerhard
  • 6,850
  • 8
  • 51
  • 81
  • You didn't actually pass the numbers to the function. You need to study the chapter about functions in your C book, where it explains parameter passing. – Lundin Mar 02 '22 at 06:59
  • 1
    And a better answer than those posted would be `void display(size_t size, int arr[size);` ... `display(size, arr);` – Lundin Mar 02 '22 at 07:01

3 Answers3

1

You have two arrays with the same name but different scope. A variable declared in a function is not visible outside of an function. In your case the one stores the numbers and the one gets printed but they are not the same. Moving int arr[size]; to global scope would resolve this problem related to scope.

#include<stdio.h>
#define size 5

int arr[size];

void display(){
     for(int i = 0; i < 5; i++)
        printf("Element[%d] = %d\n", i, arr[i]);
}
int main()
{
    for(int i = 0; i < size; i++){
        printf("Input a number at index %d: ", i);
        scanf("%d", &arr[i]);
    }
    display();
}

A better solution would be to avoid global variables and use a pointer to pass your array to the display function.

#include<stdio.h>
#define size 5

void display(int *arr){
     for(int i = 0; i < size ; i++)
        printf("Element[%d] = %d\n", i, arr[i]);
}
int main()
{
    int arr[size];
    for(int i = 0; i < size; i++){
        printf("Input a number at index %d: ", i);
        scanf("%d", &arr[i]);
    }
    display(arr);
}
Gerhard
  • 6,850
  • 8
  • 51
  • 81
  • is there an other way to solve it without using global scope? – Shark tales Mar 02 '22 at 06:44
  • While declaring `arr` global will allow the call of `display()` to work, global variables should be avoided to eliminate name collisions as the code becomes longer. Properly passing the array as a parameter will also make the function reusable. – David C. Rankin Mar 02 '22 at 06:45
  • @David C. Rankin I was focusing on variable scope and why there was no compile error while the code was not working. Using global scope is seldom the right thing to do. I updated mo answer to show the ideal answer as well. – Gerhard Mar 02 '22 at 06:50
  • No worries, just making sure the OP understood what his options were here. Nice job. – David C. Rankin Mar 02 '22 at 07:04
1

You can't pass an array, but you can pass a pointer into it instead.

void print_elements(int *array, int length) {
    for (int i = 0; i < length; i++) {
        printf("%d", array[i]);
    }
}

int foo[size];
print_elements(foo, size);
Locke
  • 7,626
  • 2
  • 21
  • 41
  • Technically you can pass either `int array[size]` or `int *array` as the array, on access, is converted to a pointer to the first element [C11 Standard - 6.3.2.1 Other Operands - Lvalues, arrays, and function designators(p3)](http://port70.net/~nsz/c/c11/n1570.html#6.3.2.1p3) Also here, it's fine to pass `length`, but since `size` is defined as `5`, it can also be omitted and `size` used within the function -- worth pointing out in your answer. – David C. Rankin Mar 02 '22 at 06:42
1

You did not pass the array as an argument to the function, functions can only access variables passed as arguments or global variables. i changed your code a bit and added some checks for invalid values, now it should work.

#include<stdio.h>
#define SIZE 5

void display(int*, int);
int main()
{
    int arr[SIZE];
    int i;

    for(i = 0; i < SIZE; i++){
        printf("Input a number at index %d: ", i);
        int res = scanf("%d", &arr[i]);
        if (res != 1) {
            printf("Not a valid integer scanned!");
            return -1;
        }
        
    }
    display(arr, SIZE);
   
  
}
void display(int* arr, int size){
    if (arr == NULL) {
        printf("Error! Null array!");
        return;
    }

     for(int i = 0; i < size; i++)
        printf("Element[%d] = %d\n", i, arr[i]);
}
Omer Kawaz
  • 291
  • 1
  • 12
  • 1
    May be worth cleaning up extra blank-lines. Also, you should speak to why you need to validate EVERY user-input. Here, you do a good job saving the `scanf()` return in `res` and validating the return. With your increment in a for-loop, it makes it almost impossible to recover from a bad input. [Example of only increment on Good input](https://stackoverflow.com/a/71318212/3422102) While that is C++, the loop structure is identical to what you can use here. – David C. Rankin Mar 02 '22 at 07:09