0

my code runs, but I get this error! can any one show me what's wrong..

I am new to c language and can any one help me with these codes.


#include <stdio.h>

void main()
{
    int arr[10],i;
    getArray(arr,i);
    displayArray(arr,i);
}

int getArray(int arr[10],int i)
{
    printf("Enter 5 numbers: \n");
    for (i=0; i<5; i++)
    {
        scanf("%d",&arr[i]);
    }
    return arr;
}

int displayArray(int arr[10], int i)
{
    for (i=0; i<5; i++)
    {
        printf("%d ",arr[i]);
    }
    return 0;
}
  • 1
    What is the return type of `getArray`? What is the type of `arr` (that you return from it)? Why are you even return `arr` from the function? Oh, and why are you passing `i` as an argument? – Some programmer dude Mar 20 '23 at 14:02
  • 1
    Also, why is `displayArray` returning a value? Why don't you declare the function before you use them? All in all I think you need to find a new beginners learning resource. – Some programmer dude Mar 20 '23 at 14:02

1 Answers1

0

You have to declare or define functons before using.

Add declaration before using:

#include <stdio.h>

/* declarations of functions */
int getArray(int arr[10],int i);
int displayArray(int arr[10], int i);

int main(void)
{
    int arr[10],i;
    getArray(arr,i);
    displayArray(arr,i);
}

int getArray(int arr[10],int i)
{
    printf("Enter 5 numbers: \n");
    for (i=0; i<5; i++)
    {
        scanf("%d",&arr[i]);
    }
    return arr;
}

int displayArray(int arr[10], int i)
{
    for (i=0; i<5; i++)
    {
        printf("%d ",arr[i]);
    }
    return 0;
}

Move definitions of functions before using:

#include <stdio.h>

int getArray(int arr[10],int i)
{
    printf("Enter 5 numbers: \n");
    for (i=0; i<5; i++)
    {
        scanf("%d",&arr[i]);
    }
    return arr;
}

int displayArray(int arr[10], int i)
{
    for (i=0; i<5; i++)
    {
        printf("%d ",arr[i]);
    }
    return 0;
}

int main(void)
{
    int arr[10],i;
    getArray(arr,i);
    displayArray(arr,i);
}

Also note that you should use standard int main(void) in hosted environment instead of void main(), which is illegal in C89 and implementation-defined in C99 or later, unless you have some special reason to use non-standard signature.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • 1
    @Jabberwocky: This answer neither explains the problem nor shows a way to solve it. The code in this answer does not compile. The error in the code in the question is inside the function `getArray`, which means the declaration of the return type and the parameters of the function is visible at the point of the error, so the problem is not caused by a missing declaration prior to the error. The key error, which this answer failed to diagnose, is that the `getArray` function attempts to return an `int *` but is declared to return an `int`. – Eric Postpischil Mar 20 '23 at 15:04
  • 1
    The fact it got two votes up shows there are voters who are not giving proper attention to the posts they are voting on. – Eric Postpischil Mar 20 '23 at 15:06