Sign in to follow this  
Greedful

Passing Array Arguments

Recommended Posts

First off it's all in C, and secondly it's not "How to pass a pointer". All I'm looking for is something simple. I want to do this:
int foo(char* function)
{
   //manipulate data;
}
int main(int argc, char* argv[])
{
   foo("abc");
}
but with an array of numerical values. In Java you could pass "new int[] {1, 2, 3}" as an argument, is there a similar syntax in C? I'm just too lazy to want to define a local variable and set the data I want in it. Sorry if this isn't too important. It's bugging the crap outta me.

Share this post


Link to post
Share on other sites
The short answer is no. No there is not. You can do this:

int my_integers[] = { 0, 1, 2, 3 };
function(my_integers);


But that's the limit.

Also, even if you could, you'd probably have a memory leak, since the new int[]{1,2,3} would allocate memory, but you'd lose your reference to it, and be unable to deallocate it.

Share this post


Link to post
Share on other sites
Well.... That sucks.

In that case: How exactly do you go about defining an array of an array? Such as "int** test = {{3, 2}, {2, 3}}"? I'm gonna go out on a limb and say that's illegal as well and that I need to predefine each array then set another array of pointers to those arrays, but if it's not - I'd love to know how.

Also, for the String example I'm allocating and referencing to the memory anyways. If I recall correctly, passing the array as a function is written into the function's local variables and is removed or unreferenced once the function returns. So for my needs it's working thus far.

Thanks for the quick reply, I'll get right on ahead to defining global variables and just referencing to them.

Share this post


Link to post
Share on other sites
What would be wrong with this:


int foo(int* arr)
{
//manipulate data;
}
int main(int argc, char* argv[])
{
int a[3] = {1, 2, 3}
foo(a);
}
[/code]

Seems to be the closest you can get. You can pass arrays as function arguments in C, by letting them decay to pointers.
It's not perfect, but it's the best you're going to get in C. And no, global variables won't be an improvement.

Share this post


Link to post
Share on other sites
Quote:
Original post by Greedful
In that case: How exactly do you go about defining an array of an array?

Multidimensional arrays are a bit tricky in C. I'm not very proficient with C, so take this with a grain of salt.

Arrays don't have values (only their elements). One effect of this is that you can't assign arrays to each other. Arrays store values in their elements. Since an array itself is not a value, arrays of arrays can't exist.

Quote:
Original post by Greedful
Such as "int** test = {{3, 2}, {2, 3}}"?

Initialization is a different thing. That thingy on the right side with the curly braces is an initializer list, not an array literal. C++0x will allow using initializer lists as literals, but C does not.

BTW your code doesn't compile, because pointers and arrays aren't the same thing. Try this instead:

int test[][2] = {{3, 2}, {2, 3}};


Quote:
Original post by Greedful
I need to predefine each array then set another array of pointers to those arrays

An array of pointers and a twodimensional array are different beasts.

Quote:
Original post by Greedful
Also, for the String example I'm allocating and referencing to the memory anyways.

String literals are a special case.

Share this post


Link to post
Share on other sites
If you're not afraid of variadic macros (a C99 feature), you can do something like this:


#include <stdio.h>

#define pass_array(fun, type, ...) { type __temp[] = {__VA_ARGS__}; fun(__temp, sizeof __temp / sizeof(type)); }

void foo(int *a, int n)
{
while (n--) printf("%d\n", *a++);
}

int main(void)
{
pass_array(foo, int, 25, 16, 9);
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Spoonbender
Seems to be the closest you can get. You can pass arrays as function arguments in C, by letting them decay to pointers.
It's not perfect, but it's the best you're going to get in C. And no, global variables won't be an improvement.

Eh, I was just trying to be lazy with my global variables. So global variables will be an improvement.
Quote:
Original post by DevFred
BTW your code doesn't compile, because pointers and arrays aren't the same thing. Try this instead:

int test[][2] = {{3, 2}, {2, 3}};

Well that's kinda what I wanted... An array of pointers to arrays. Once again, I was just being lazy. I'm a bit obsessive compulsive with organization and that would have spared up a few lines. I'll try out the macro, looks like fun.

Thanks for all the replies everyone. I'll try to brush up on my programming lingo, seems like it's relatively easy to misinterpret what I say.

Share this post


Link to post
Share on other sites
Quote:
Original post by Greedful
Eh, I was just trying to be lazy with my global variables. So global variables will be an improvement.


Global variables are completely orthogonal to the problem at hand: they solve none of the issues present in code without the global variables, as far as passing a temporary array to a function goes.

Quote:
Well that's kinda what I wanted... An array of pointers to arrays. Once again, I was just being lazy. I'm a bit obsessive compulsive with organization and that would have spared up a few lines. I'll try out the macro, looks like fun.


No, that would be a multi-dimensional array, not an array of pointers to arrays. Be careful, the two are not equivalent.

If you're that obsessive about sparing lines, use a highly expressive language, such as Python, PHP or an ML variant. For example:

/* PHP   */ foo( array( 1, 2, 3) );
(* OCaml *) foo [| 1; 2; 3 |]

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Global variables are completely orthogonal to the problem at hand: they solve none of the issues present in code without the global variables, as far as passing a temporary array to a function goes.

No no no, it's not a temporary array. I was only trying to allocate memory for an array by passing it an argument used for array construction. Like I did with the String. I did properly use malloc in my function along with a few arguments, so there's no need to point that out.
Quote:
Original post by ToohrVyk
No, that would be a multi-dimensional array, not an array of pointers to arrays. Be careful, the two are not equivalent.

The example I used was just something I whipped together because I didn't know the appropriate syntax. I would still like to know how to create an array of pointers to arrays. I would know the sizes and whatnot. It'd be something like:

int array1[] = {1, 2};
int array2[] = {3, 4};
int* array[] //I'm assuming you'd need to malloc here, unless there's some sort of syntax that allows you to define 2 indexes for the array of pointers.

Quote:
Original post by ToohrVyk
If you're that obsessive about sparing lines, use a highly expressive language, such as Python, PHP or an ML variant. For example:

/* PHP   */ foo( array( 1, 2, 3) );
(* OCaml *) foo [| 1; 2; 3 |]

Obsessive AND lazy mind you, but I like my C.

Share this post


Link to post
Share on other sites
Quote:
Original post by Greedful
The example I used was just something I whipped together because I didn't know the appropriate syntax. I would still like to know how to create an array of pointers to arrays. I would know the sizes and whatnot.


// Function which takes an array of pointers to integer arrays
void foo(int (*(*arr)[N])[M]) {
(*(*arr)[i])[j];
}

int arr1[N] = { 1, 2 };
int arr2[N] = { 3, 4 };
int ((*arr)[M])[N] = { &arr1, &arr2 };

foo(&arr);


Yes, I probably got the syntax right at the last step. I hate this [smile] Of course, it could be much simpler to use pointer-to-pointer-to-integer instead:

// Function which takes a pointer-to-pointer-to-integer
void foo(int ** arr) {
arr(i][j];
}

int arr1[] = { 1, 2, 3, 4 };
int *arr2[] = { arr1, arr1 + 2 };

foo(arr2);



Quote:
Obsessive AND lazy mind you, but I like my C.
How girlish [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Yes, I probably got the syntax right at the last step. I hate this [smile] Of course, it could be much simpler to use pointer-to-pointer-to-integer instead

Oh simple now? I like simple. Much appreciated.
Quote:
Original post by ToohrVyk
How girlish [smile]

Sir, that made my day. Thank you.

Share this post


Link to post
Share on other sites
Can you take a step back from all those arrays and pointers and tell us what problem you are trying to solve? What does this data structure you are trying to pass represent? What are you going to do with it?

Share this post


Link to post
Share on other sites
Quote:
Original post by Greedful
I'll try out the macro, looks like fun.

Forget the macro hackery, C99 has compound literals. Here is an example:

(int []){ 47, 11 }

You can even create anonymous arrays with values that are not compile time constants. Hooray!

#include <stdio.h>

void foo(int *p, int n)
{
while (n--) printf("%d\n", *p++);
}

int main(void)
{
foo((int []){ 47, 11 }, 2);

int x, y, z;
scanf("%d%d%d", &x, &y, &z);
foo((int []){ x, y, z }, 3);
}



Is C99 awesome or what? :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this