Home > other >  I have to return back an array from a function and print it in the main function but I am unable to
I have to return back an array from a function and print it in the main function but I am unable to

Time:03-01

This code returns the new array to the main function using the pointer, but then as I do not know the size of the returned array , i am not able to print only the required numbers and also garbage values are getting printed, so I decided to print numbers which are not null , but the pointer is not getting assigned to integer and is also not being dereferenced. what to do ?

#include <stdio.h>

int *geteven_Arr(int *l, int r)
{
    static int a[10];
    int o = 0;

    for (int i = 0; i < r; i  )
    {
        if (l[i] % 2 == 0)
        {
            a[o] = l[i];
            o  ;
        }
    }

    return a;
}

int main()
{
    int i, n;
    int *m;
    printf("Enter the number of elts(less than 10) in array\n");
    scanf("%d", &n);
    int arr[n];
    printf("Enter the elts\n");

    for (int i = 0; i < n; i  )
    {
        scanf("%d", &arr[i]);
    }

    m = (geteven_Arr(arr, n));

    for (int i = 0; i < n; i  )
    {
        if ((*(m[i]) != 0))
            printf("%d\t", m[i]);
    }

    return 0;
}

CodePudding user response:

You could terminate the array with a NULL, or you can pass in a double pointer and return the size.

Also, static memory is going to get you into trouble; each call is returning the same pointer. Malloc it.

The first...

int *geteven_Arr(int *l, int r)
{
    // Allocate as much as you might need
    // and one for the null.
    int a = malloc(sizeof(int*) * (r 1));
    int o = 0;

    for (int i = 0; i < r; i  )
    {
        if (l[i] % 2 == 0)
        {
            a[o] = l[i];
            o  ;
        }
    }

    a[o] = NULL;

    return a;
}

Then iterate until null.

int *m = geteven_Arr(arr, n);
for (int i = 0; *m[i] != NULL; i  ) {
    printf("%d\n", *m[i]);
}
free(m);

Or you pass in a double pointer to use as the return value.

size_t geteven_Arr(int *l, int r, int **a)
{
    *a = malloc(sizeof(int*) * (r 1));
    size_t o = 0;

    for (int i = 0; i < r; i  )
    {
        if (l[i] % 2 == 0)
        {
            a[o] = l[i];
            o  ;
        }
    }

    return o;
}

And call it like so.

int *m = NULL;
size_t m_len = geteven_Arr(arr, n, &m);
for (int i = 0; i < m_len; i  ) {
    printf("%d\n", *m[i]);
}
free(m);

(I'm on a phone, there might be small errors)

CodePudding user response:

The primary issue is you r using dereference operator (*) twice, don't use * before m[i] because m is a pointer which has already stored or referenced to the address of array a which was returned by the function. In other words what u r doing is taking the address of address of a. It's a bit confusing I know but that's how I think it is. About the garbage value you can use NULL or '\0' at the end of array manually. I have made changes in your program hope it will help you. Sayonara...

#include <stdio.h>

int *geteven_Arr(int *l, int r)
{
int i;
static int a[10];
int o = 0;

for (i = 0; i < r; i  )
{
    if (l[i] % 2 == 0)
    {
        a[o] = l[i];
        o  ;
    }
}
a[i] = '\0'; // USE NULL OPERATOR
return a;
}

int main()
{
int n;
int *m;
printf("Enter the number of elts(less than 10) in array\n");
scanf("%d", &n);
int arr[n];
printf("Enter the elts\n");

for (int i = 0; i < n; i  )
{
    scanf("%d", &arr[i]);
}

m = (geteven_Arr(arr, n));

for (int i = 0; i < n; i  )
{
    if (((m[i]) != 0)) // DON'T USE DEFERENCE OPERATOR
        printf("%d\t", m[i]);
}
 printf("\n");
 return 0;
}
  •  Tags:  
  • c
  • Related