1

A function cannot return an array, only a pointer to an array. So I tried doing this:

int *arr = {-1, -1};

Is that valid syntax? I got a warning from the compiler for that. If not, is there a better way of setting a pointer variable to an array without looping?

3
  • 1
    In C99 do: void* f(){ int size = sizeof(int)*2; return memcopy(malloc(size), (int []){2, 4};, size); } Commented Feb 14, 2014 at 15:24
  • Check this code Commented Feb 14, 2014 at 15:32
  • 1
    You really should read up on the differences between pointers and arrays. But basically, setting a pointer to an array is as easy as int *ptr = &arr[0]; or even int *ptr = arr;. Commented Feb 14, 2014 at 15:44

5 Answers 5

6

No. This is not a valid syntax. A valid syntax which could be a pointer to a compound literal is

int *arr = (int []){-1, -1};  // Valid only in C99 and latter

If you wanna return an array then return a pointer to it;

int *arr = malloc(sizeof(*arr)*2);
....
return arr;
Sign up to request clarification or add additional context in comments.

12 Comments

So in this case is there a shortcut way to initialize the array without looping?
A valid declaration in c99 can be int *arr = (int*){-1, -1};
@GeorgeNewton you can use memcpy(arr, (int []){ -1, -1}, sizeof(int)*2); But still you have to use malloc to allocate memory if you wants to return from function.
@haccks No we can't it has life within function only.
Doing int * arr = malloc(2 * sizeof(*arr)); would be saver.
|
4

You can set a pointer to an array created on the fly with a compound literal:

int *p = (int []){2, 4};

However, the array lives only as long as the block it is in. You cannot return it from a function. To return an array (by pointer), you should allocate it dynamically, as with malloc.

7 Comments

Why can't I return it/
@GeorgeNewton do: int* f(){ int* p = malloc(sizeof(int)*2); memcopy(p, (int []){2, 4};, sizeof(int)*2); return p;}
@GeorgeNewton: Every C object has a lifetime. The lifetime of automatic objects does not extend beyond the execution of the function they are created in (and may end earlier, as when a block finishes executing). Since the object does not exist (in the C model) after the function returns, returning a pointer to it is inappropriate; the pointer would be pointing to space that should not be accessed.
So everytime I make in array inside a function, it disappears even if I set a pointer variable to it? I absolutely have to malloc for it to stay?
@GeorgeNewton - If you live through the painful phases of learning it, C is likely the single most portable, versatile, universally useful languages you will find. It is used in more areas of programming than any other single language I am aware of: application, library (both user mode and kernal mode), embedded,... et. al. and on more OS's. Windows, linux, Mac,... So, stay with it.
|
2

Is there a better way of setting a pointer variable to an array without looping?

There is a option called pointer to an array.
sample:

int a[10];
int (*x)[10] = &a;

Comments

0

arr is pointer to integer. So you can not have multiple inits like this.

You can have below:

int *arr = NULL;

arr = malloc(..)

...
...
arr[0] = some value..
arr[1] = some value..

Comments

0

Regarding

int *arr = {-1, -1};

Is that valid syntax?

No, but this is:

int *arr = {0}; //initialize pointer to 0;

arr = malloc(100*sizof(int));// allocate memory (can also do this at creation of arr) 

arr[99]=1; //assign value to elements as needed  

free(arr);// free memory when done with arr  

And - in this form (pointer), arr is returnable by a function:

int * somefunc(int *arr)
{
    //manipulate arr[0] - arr[n]
    ....
    return arr;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.