0

I have two integer arrays created at runtime (size depends on the program input). At some point I need to update the contents of an array with the contents of the other doing some calculations.

First I thought about passing those arrays as parameters to a function because I didn't find a way to return functions in C (don't think it's possible). After realizing that was a bad idea since parameters are not really modifiable as they're copied to the stack I resorted to change to array pointers instead.

While the function is still empty, this is the code I have:

1st take (code compiles, no errors):

// Elements is just to be able to iterate through their contents (same for both):
void do_stuff(int first[], int second[], int elements) {}

// Call to the function:
do_stuff(first, second, elements);

2nd take, attempt to translate to pointers to be able to modify the arrays in place:

void do_stuff(int *first[], int *second[], int elements) {}

// Call to the function:
do_stuff(&first, &second, elements);

This code lead to some rightful compile time errors, because apparently what I thought to be pointer to arrays were arrays of pointers.

3rd take, what I think it'd be the right syntax:

void do_stuff(int (*first)[], int (*second)[], int elements) {}

// Call to the function:
do_stuff(&first, &second, elements);

Still this code produces compile time errors when trying to access the elements of the arrays (e.g. *first[0]):

error: invalid use of array with unspecified bounds

So my question is regarding the possibility of using an array pointer as a parameter of a function, is it possible? If so, how could it be done?

Anyway, if you think of a better way to update the first array after performing calculations involving the contents of the second please comment about it.

1
  • When you pass an array as a function argument, in fact you pass a pointer to the first element, so you can modify the array from the function using that pointer. Commented Aug 29, 2012 at 1:27

3 Answers 3

2

An array decays to a pointer to the data allocated for the array. Arrays are not copied to the stack when passing to functions. Thus, you needn't pass a pointer to the array. So, the below should function fine.

// Elements is just to be able to iterate through their contents (same for both):
void do_stuff(int first[], int second[], int elements) {}

// Call to the function:
do_stuff(first, second, elements);

The cause of your errors on your second attempt are because int *first[] (and the others like it) are actually of the type array of pointer to int.

The cause of your third errors are because *first[N] is actually *(first[N]), which cannot be done with ease. Array access is really a facade over pointer arithmetic, *(first + sizeof first[0] * N); however, you have an incomplete element type here -- you need to specify the size of the array, otherwise sizeof first[0] is unknown.

Sign up to request clarification or add additional context in comments.

Comments

1

Your first attempt is correct. When passing an array as a parameter in C, a pointer to the first element is actually passed, not a copy of the array. So you can write either

void do_stuff(int first[], int second[], int elements) {}

like you had, or

void do_stuff(int *first, int *second, int elements) {}

Comments

0

In C arrays automatically decay to pointers to data, So, you can just pass the arrays and their lengths and get the desired result.

My suggestion is something like this:

void dostuff(int *first, int firstlen, int *second, int secondlen, int elements)

Function call should be:

 do_stuff(first, firstlen, second, secondlen, elements);

I am not very clear from your question, why you need elements. But, you must pass array lengths as arrays automatically decays to pointers when passed to a function, but, in the called function, there is no way to determine their size.

1 Comment

elements designated the array lengths, I realize length is much clearer; thanks.

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.