1

I am getting error whith the code bellow. Could you please help me to correct this? I am beginner in programming.

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

int array1(int);
int main(void)
{
    int num, array[100][100], ys, i, j;
    setbuf(stdout, NULL);
    printf("Print Fist Array Value");
    scanf("%d", &num);
    array[][] = array1(num);
    printf("Do you want to print First Array");
    scanf("%d", &ys);
    if (ys == 1)
    {
        for (i = 0; i < num; i++)
        {
            for (j = 0; j < num; j++)
            {
                printf(array[i][j]);
            }
        }
    }
    else
    {
        printf("Thanks, Not Printed");
    }

    return EXIT_SUCCESS;
}
int array1(int val)
{
    int i, j, Array1[100][100];
    printf("Enter First Array Values");
    for (i = 0; i < val; i++)
    {
        for (j = 0; j < val; j++)
        {
            scanf("%d", &Array1[i][j]);
        }
    }
    return Array1;
}

Seeing the below error when building the project:

..\src\FunctionWithArray.c: In function 'array1': ..\src\FunctionWithArray.c:42:9: warning: returning 'int (*)[100]' from a >function with return type 'int' makes integer from pointer without a cast >[-Wint-conversion] return Array1; ^~~~~~ ..\src\FunctionWithArray.c:42:9: warning: function returns address of local variable [-Wreturn-local-addr]

23:34:36 Build Failed. 1 errors, 3 warnings. (took 205ms)

anastaciu
  • 23,467
  • 7
  • 28
  • 53
Ramsid Nv
  • 11
  • 1
  • 1
    You can't assign arrays. Also you can't return locally defined arrays from functions. Please fix the indentation, this code is impossible to read,. – Eugene Sh. Mar 29 '21 at 19:52
  • I cannot call function with return array value? – Ramsid Nv Mar 29 '21 at 19:58
  • @RamsidNv no, the array is local, you'd do better passing it as an argument of the function. – anastaciu Mar 29 '21 at 20:04
  • @anastaciu I didnt get, can you please explain? please note that i just started to learn C programming. – Ramsid Nv Mar 29 '21 at 20:09
  • 1
    @RamsidNv Ok, I answered the question explaining the problem(s). Since you are a beginner, if you are interested in learning the language with a solid base let me refer you to [The Definitive C Book Guide and List](https://stackoverflow.com/q/562303/6865932) – anastaciu Mar 29 '21 at 20:51

1 Answers1

3

There are some issues in your code that need to be fixed, first and foremost returning a local array is a bad idea as the compilation error suggests, its lifetime expires when the function returns, accessing it after that, even if you fixed the assignment, which can't be done like that, invokes undefined behavior. Aside from that there are some other minor issues fixed and commented in the code bellow:

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

void array1(int val, int Array1[][100]); // pass the array as argument
int main(void)
{
    int num, array[100][100], ys, i, j;

    setbuf(stdout, NULL);
    printf("Print Fist Array Value: ");

    if(scanf("%d", &num) != 1) // it's always a good idea to guard against input problems
    {
        // deal with bad input
    }

    array1(num, array); // execute the function, no assignment needed
    printf("Do you want to print First Array? ");
    scanf("%d", &ys);
    if (ys == 1)
    {
        for (i = 0; i < num; i++)
        {
            for (j = 0; j < num; j++)
            {
                printf("%d ", array[i][j]); // format specifier needed
            }
            putchar('\n');
        }
    }
    else
    {
        printf("Thanks, Not Printed!");
    }
    return EXIT_SUCCESS;
}
void array1(int val, int Array1[][100])
{
    int i, j;
    printf("Enter First Array Values: ");
    for (i = 0; i < val; i++)
    {
        for (j = 0; j < val; j++)
        {
            scanf("%d", &Array1[i][j]);
        }
    }
}
anastaciu
  • 23,467
  • 7
  • 28
  • 53