1

function for get array from the user

#include <stdio.h>
void getArray()
{
   
    
     printf("Enter size of array: ");
    scanf("%d",&n);
 
     printf("Enter %d elements in the array : ", n);
    for(i=0;i<n;i++)
    {
        scanf("%d", &a[i]);
    }
    }

function for display array

void displayArray(){
    
    printf("\nElements in array are: ");
    for(i=0;i<n;i++)
 
    {
        printf("%d  ", a[i]);
    }
}

Both functions are called in the main

int main(){
    int a[1000],i,n;
    getArray();
     displayArray();
    return 0;
}

The problem is how to pass the array that we get from the user to the display array function and both functions can be called in the main and also the array want to declare in the main function

ABIN
  • 29
  • 5
  • The usual: `scanf` of (potentially malformed) user input without checking return value. Use leading whitespace in format string to skip any number of whitespace in input. Check the number of elements against the size of your array to avoid overflow. – DevSolar Oct 01 '22 at 12:11
  • what you mean can you explain that – ABIN Oct 01 '22 at 12:13
  • Usually, we pass the address of the first element of the array as an argument to the function that processes it. It is better to pass him his size too. – CGi03 Oct 01 '22 at 12:15
  • It will be very helpful for me if you can explain with code – ABIN Oct 01 '22 at 12:18
  • Try [how to read / parse input in C](https://stackoverflow.com/questions/35178520/how-to-read-parse-input-in-c-the-faq) – DevSolar Oct 01 '22 at 12:21

2 Answers2

1

An example that does not handle input errors. In order for your functions to have knowledge of the array, you must send them its address as well as its size.

#include <stdio.h>

int getArray(int a[], int size_max)
{
    int n;
    printf("Enter size of array: ");
    while(1)
    {
        scanf("%d",&n);
        if(n>size_max) printf("The size must be less than %d: ", size_max);
        else break;
    }

    printf("Enter %d elements in the array : ", n);
    for(int i=0; i<n; i++) scanf("%d", &a[i]);

    return n;
}

void displayArray(int a[], int n)
{
    printf("\nElements in array are: ");
    for(int i=0; i<n; i++) printf("%d  ", a[i]);
}

int main()
{
    int a[1000];
    int n = getArray(a, 1000);
    displayArray(a, n);
    return 0;
}
CGi03
  • 452
  • 1
  • 3
  • 8
0

You can pass that shared variable as argument. Also return and use the returned value if reference not having valid data. Or else you need to pass this argument as reference instead of value like this.

#include <stdio.h>
int[] getArray(int a[])
{
 printf("Enter size of array: ");
scanf("%d",&n);

 printf("Enter %d elements in the array : ", n);
for(i=0;i<n;i++)
{
    scanf("%d", &a[i]);
}
return a;
}

function for display array

void displayArray(int a[]){

printf("\nElements in array are: ");
for(i=0;i<n;i++)

{
    printf("%d  ", a[i]);
}
}

Both functions are called in the main

int main(){
    int a[1000],i,n;
    a = getArray(a);
     displayArray();
    return 0;
}