1

How do I pass an array from a function to main in C?

#include<stdio.h>
int merge(int a[],int b[])
{
    int i=0;
    int j=0;    
    int k=0;
    int c[17];
    while(i<10&&j<8)
    {
        if(a[i]<b[j])
        {

            c[k]=a[i];
            i++;
        }
        else
        {
            c[k]=b[j];
            j++;
        }
        k++;
    }
    while(i<10)
    {
        c[k]=a[i];
        i++;
        k++;
    }
    while(j<10)
    {
        c[k]=b[j];
        j++;
        k++;
    }
    return c;
}
Cacho Santa
  • 6,846
  • 6
  • 41
  • 73
SID_J
  • 23
  • 3
  • Also. Your question did not make sense. You want that your function return an array? – André Puel Feb 10 '13 at 22:23
  • sir my moto is to combine 2 arrays and return the new array to the main function.. – SID_J Feb 10 '13 at 22:25
  • i hv made the indentations proper but somhow the codes are not readabe propperly on the site .. – SID_J Feb 10 '13 at 22:27
  • You can pass pointers around quite easily. How is your attempt failing? – John Dvorak Feb 10 '13 at 22:27
  • sorry for inconvinience.. – SID_J Feb 10 '13 at 22:27
  • I guess the `merge` method will _always_ overrun the `c` array, causing undefined behavior. – John Dvorak Feb 10 '13 at 22:28
  • Also, you are returning a pointer to something allocated on the stack. That's bad. – John Dvorak Feb 10 '13 at 22:30
  • 1. Your array is local to the function it is declared in and references to it are invalid once the function returns. 2. You have declared the function to return an int, yet you are attempting to return a pointer to an int (you cannot return arrays from functions, they degrade to pointers). 3. Your loops are unsafe, you have no idea how large the input arrays are. 4. This is a very confused question in general and your description is lacking. – Ed S. Feb 10 '13 at 22:31

3 Answers3

2

One solution is to take three array parameters:

void merge(int a[], int b[], int c[]);

Then you can merge a and b and put the result into c. The person who calls the function is responsible for creating the array used as c to be large enough.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
1

If you want your function to return the array there are 2 ways:

  1. The function has to return a pointer to the first item in the array (actually the array itself is a pointer in C). In this case your function must be declared as int *merge(int a[], int b[]).
  2. You have to pass the array to the function that will be modified by the function itself. Here's how you can do this: int merge(int A[], int B[], int C[]).
siannone
  • 6,617
  • 15
  • 59
  • 89
1

Here are three ideas...

  1. You can't return arrays in C but you can return a struct that contains an array, so you could do that.
  2. If you just say int x[3]; return x; that will return the address of x. That's OK if it's static or malloc-ed but will lose horribly if it's an ordinary auto-storage-class local. So whatever you do, you should malloc(3) the object in the function, or at least make it static.
  3. You could pass the new array as a parameter, thus avoiding the issue of returning it.
DigitalRoss
  • 143,651
  • 25
  • 248
  • 329