1

I am working on a very basic program where I want to return an integer array of length 2 to my main block. I can't get it to work though, and I was told that I may need pointers to do this. How do pointers work, and how can I use this in my program? Here is my current code:

int[] return2();


int main() {

  int a[2];

  a = request();
  printf("%d%d\n", a[0], a[1]);


  return(0);
}

int[] request ()
{
  int a[2];

  a[0] = -1;
  a[1] = 8;

  return a;
}
osgx
  • 90,338
  • 53
  • 357
  • 513

6 Answers6

6
  • You can't declare a function returning an array.

    ISO/IEC 9899:1999

    §6.9.1 Function definitions

    ¶3 The return type of a function shall be void or an object type other than array type.

    C2011 will say essentially the same thing.

  • You shouldn't ever return a pointer to a (non-static) local variable from a function as it is no longer in scope (and therefore invalid) as soon as the return completes.

You can return a pointer to the start of an array if the array is statically allocated, or if it is dynamically allocated via malloc() et al.

int *function1(void)
{
    static int a[2] = { -1, +1 };
    return a;
}

static int b[2] = { -1, +1 };

int *function2(void)
{
    return b;
}

/* The caller must free the pointer returned by function3() */
int *function3(void)
{
    int *c = malloc(2 * sizeof(*c));
    c[0] = -1;
    c[1] = +1;
    return c;
}

Or, if you are feeling adventurous, you can return a pointer to an array:

/* The caller must free the pointer returned by function4() */
int (*function4(void))[2]
{
    int (*d)[2] = malloc(sizeof(*d));
    (*d)[0] = -1;
    (*d)[1] = +1;
    return d;
}

Be careful with that function declaration! It doesn't take much change to change its meaning entirely:

int (*function4(void))[2]; // Function returning pointer to array of two int
int (*function5[2])(void); // Array of two pointers to functions returning int
int (*function6(void)[2]); // Illegal: function returning array of two pointers to int
int  *function7(void)[2];  // Illegal: function returning array of two pointers to int
Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • +1 : Didn't know that static can be used in functions. I thought that it can only be used in classes(C++). – Careal Manic Mar 12 '13 at 04:08
  • This is C, not C++. I hope the "Don't know" is really "Didn't know", and that now you do know. In fact, you can use `static` like that (inside a function) in C++ too. – Jonathan Leffler Mar 12 '13 at 04:10
3

You better to understand how does pointer work. Here is a (bad) solution:

#include <stdio.h>

int* request(){
    int a[2];
    a[0] = -1;
    a[1] = 8;
    return a;
}

int main() {
    int* a;
    a = request();
    printf("%d%d\n", a[0], a[1]);
    return 0;
}

but there is a problem. since int a[2]; in int* request() is a local variable, there is no guarantee that the value returned will not be overwritten. Here is a better solution:

#include <stdio.h>

void request(int* a){
    a[0] = -1;
    a[1] = 8;
}

int main() {
    int a[2];
    request(a);
    printf("%d %d\n", a[0], a[1]);
    return 0;
}
Careal Manic
  • 212
  • 2
  • 4
3

You have to return pointer to array of 2 integers from function. #include

int(* request()) [2]
{
static int a[2];
a[0] = -1;
a[1] = 8;
return &a;
}

int main() {
int (*a) [2];
a = request();
printf("%d%d\n", *(*a+0), *(*a+1));
return 0;
}
Hitesh Menghani
  • 977
  • 1
  • 6
  • 14
1

The array int a[2] that you declare in request is only valid during the scope of that function, so returning it like that doesn't work, since once main gets its hands on it the array is no longer valid.

If you don't understand pointers, you're going to kind of have to to really get what's going on, but here is some code that will do what you want:

int* request();

int main() {

  int* a;  // a is a pointer to an int

  a = request();
  printf("%d%d\n", a[0], a[1]);

  // we have to tell the program that we're done with the array now
  free(a);

  return(0);
}

int* request ()
{
  // allocate space for 2 ints -- this space will survive after the function returns
  int* a = malloc(sizeof(int) * 2);/

  a[0] = -1;
  a[1] = 8;

  return a;
}
alecbz
  • 6,292
  • 4
  • 30
  • 50
1

You can do as others suggest and allocate memory via malloc to return a pointer. You should also consider something like the following:

struct values {
    int val1;
    int val2;
};

struct values request();

int main() {

  struct values a;

  a = request();
  printf("%d%d\n", a.val1, a.val2);

  return(0);
}

struct values request ()
{
  struct values vals;

  vals.val1 = -1;
  vals.val2 = 8;

  return vals;
}

Structures are passed and returned by value (meaning they are copied) and can sometimes be easier and safer depending on the type of data contained within the structures.

acarlow
  • 914
  • 1
  • 7
  • 13
0

To return the array, you must dynamically allocate it.

Try this instead (Observe the changes) :

int* request();

int main() {    
  int *a;    
  a = (int *)request();
  printf("%d %d\n", a[0], a[1]);        
  return(0);
}

int* request() {
  int *a = malloc(sizeof(int) * 2);    
  a[0] = -1;
  a[1] = 8;    
  return a;
}
loxxy
  • 12,990
  • 2
  • 25
  • 56