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;
}
4
  • sorry - "request" should say return2 Commented Mar 12, 2013 at 3:55
  • If you want to know how pointers work, why not try Googling it? Commented Mar 12, 2013 at 3:56
  • Yes, you will need an understanding of pointers. stackoverflow.com/questions/897366/… Commented Mar 12, 2013 at 3:59
  • 1
    (1) learn pointers (2) if you still want to do this as a by-val result, then stuff the result content in a custom struct, which you can return by-value. Thats your call. Commented Mar 12, 2013 at 4:02

6 Answers 6

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
Sign up to request clarification or add additional context in comments.

2 Comments

+1 : Didn't know that static can be used in functions. I thought that it can only be used in classes(C++).
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.
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;
}

Comments

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;
}

11 Comments

You'd have to return &a, wouldn't you? And you've got the pointer to a local (non-static) array problem...
I seems that returning a is right. a is a pointer already. EDIT: tested. You better to return a . returning &a generates a compiler warning.
@CarealManic: did you try compiling the code? The name a is an array; when it is used in return a;, it returns a pointer to the first element of the array (an int * in this context), which is quite different from a pointer to an array (int (*)[2]) — even though the address returned, viewed as a void *, is the same.
never mind. I have misread the code. I thought that it's int* request() .
@CarealManic , this is the error i get when i compile the above program error: expected identifier or ‘(’ before ‘)’ token
|
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;
}

Comments

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.

Comments

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;
}

1 Comment

Or return a pointer to statically allocated memory...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.