1

A program to accept an array and diplay it on the console using functions. Program should contain 3 functions including main() function. Next two functions for accepting values to the array, and display array values on the console respectively.

#include <stdio.h>
#include <stdlib.h>

void getarray(int a[],int size);
void displayarray(int a[],int size);

int main(void) {
    int a[20],size;
    getarray(a,size);
    displayarray(a,size);
    return EXIT_SUCCESS;
}

void getarray(int a[],int size){
    int i;

    printf("enter the size of the array");
    scanf("%d",&size);

    for(i=0;i<size;i++){
        scanf("%d",&a[i]);
    }
}

void displayarray(int a[],int size){
    int i;

    for(i=0;i<size;i++){
        printf("%d\t",a[i]);
    }
}

This is what I tried. I did'nt get proper output.

My output:

Enter the size of the array3
1
2
3

And stopped here.

What are the mistake in it? Or is there another way to get result?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Amalshanth
  • 29
  • 1
  • 7
  • 2
    You can't use `size` in that way. The `int size` in `main` remains unininitialised throughout. So function `displayarray` has undefined behaviour. – Weather Vane Jul 20 '23 at 08:33
  • OT: don't put blank lines between each and every line in your code. I edited your code. – Jabberwocky Jul 20 '23 at 08:37

3 Answers3

4

You need to send in an int* to getarray otherwise the size variable will be local to the function and the value you fill in will not be available at the callsite:

void getarray(int a[], int *size) { // note: int*
    int i;

    printf("enter the size of the array");

    if (scanf("%d", size) != 1)    // it's already a pointer so no &
        *size = 0;

    for (i = 0; i < *size; i++) {  // dereference to get the value
        scanf("%d", &a[i]);
    }
}

Then call it like so:

getarray(a, &size);
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • Then i have a doubt. On the top where declaring the function, void getarray(int a[], int size);. If i needed to change the size variable to *size here too? – Amalshanth Jul 20 '23 at 09:05
  • @Amalshanth Yes you must. The declaration should match the definition. – Ted Lyngmo Jul 20 '23 at 09:09
  • Yeah its working.. Thankyou.. And i noticed a little mistake on the code you given.you written scan("%d", size). I think there was a missing of '&' – Amalshanth Jul 20 '23 at 09:39
  • @Amalshanth No, as I mentioned in a comment, it is a pointer already so no `&` should be used – Ted Lyngmo Jul 20 '23 at 09:47
  • But I used that & and still working. So isnt it necessary? – Amalshanth Jul 20 '23 at 10:15
  • @Amalshanth using `scanf("%d", &size)` when `size` is an `int*` is wrong. You then supply an `int**` to `scanf` which is not the correct type for `%d` so the program will have undefined behavior. – Ted Lyngmo Jul 20 '23 at 10:19
2

As stated in comments, the problem is that size in main and size in getarray are different variables, i.e. changing size inside the function doesn't change it in main.

The answer from @TedLyngmo shows a way to solve the problem using pointers.

Another solution is to let getarray return the size. And perhaps use an unsigned type for size.

unsigned getarray(int a[]) {
    unsigned i, size;

    printf("enter the size of the array");

    if (scanf("%u", &size) != 1) return 0;

    for (i = 0; i < size; i++) {
        scanf("%d", &a[i]);
    }
    return size;
}

and call it like

size = getarray(a);

BTW: For a real program you should also add code to handle cases where a user inputs a size that are too big for the array.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
1

You already defined an array with 20 elements

int a[20],size;

So this prompt

printf("enter the size of the array");

does not make sense. Also you are passing the uninitialized variable size to the functions. As a result calling the second function invokes undefined behavior.

In your description of the assignment there is written

Next two functions for accepting values to the array, and display array values on the console respectively.

It means that the array should be already defined. So this prompt

printf("enter the size of the array");

should be at least in main.

The program can look the following way

#include <stdio.h>
#include <stdlib.h>

void getarray( int a[], size_t size );
void displayarray( const int a[], size_t size );

int main(void) 
{
    size_t size;

    printf( "enter the size of the array: " );
    
    if ( scanf( "%zu", &size ) != 1 ) return EXIT_FAILURE;
    
    int a[size];

    getarray( a, size );
    displayarray( a, size );

    return EXIT_SUCCESS;
}

void getarray( int a[], size_t size )
{
    puts( "enter elements of the array:" );

    for ( size_t i = 0; i < size; i++ )
    {
        scanf( "%d", &a[i] );
    }
}

void displayarray( const int a[], size_t size )
{
    for ( size_t i = 0; i < size; i++ )
    {
        printf( "%d\t", a[i] );
    }
    putchar( '\n' );
}

A;ternatively you can dynamically allocate an array using malloc as for example

int main(void) 
{
    size_t size;

    printf( "enter the size of the array: " );
    
    if ( scanf( "%zu", &size ) != 1 ) return EXIT_FAILURE;
    
    int *a = malloc( size * sizeof( int ) );

    if ( a == NULL ) return EXIT_FAILURE;

    getarray( a, size );
    displayarray( a, size );

    free( a );

    return EXIT_SUCCESS;
}

The other two funcions stay unchanged.

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