3

Apologies if this is a stupid question, but I'm a newbie and couldn't find the answer anywhere. I've declared an array of five objects (Fraction objects) like so:

Fraction *fractArray[5];

I'd now like to pass this array (or a pointer to it, at least) to a C-style function that will do various things to the fractions in the array and return a single fraction. After declaring a pointer to the array (call it *fractPointer) I've tried to set up a function like:

Fraction* arraySum (Fraction *fractPointer, int arraySize)
{
    // dostuff , then return a fraction as the result
}

...and pass it the pre-defined pointer and the array size. I've established through debugging that the fractPointer successfully points at the first array element. The pointer is also pointing to the right place initially in the function. But I can't seem to get the pointer to increment to the next object in the array. It keeps throwing up errors when I do fractPointer++; (same thing when declaring a new Fraction pointer within the function). The complaint is that I'm doing arithmetic on a pointer to Fraction, which is not of a constant size. Am I going about this all wrong?

2 Answers 2

2

Arrays decay into pointers when passed to functions. So you need to declare your function like this:

Fraction *arraySum (Fraction **fractPointer, int arraySize);

or as

Fraction *arraySum (Fraction *fractPointer[], int arraySize);

in order to get it working.

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

1 Comment

Thanks! the first syntax works quite well. I clearly need to improve my understanding of pointers and arrays re: functions.
-1

Note how you declared your array: Fraction *fractArray[5]. Another way to write that would be Fraction **fractArray. An array of pointers. So to pass that array to another function, you need to use the same type - either Fraction** or Fraction *foo[].

This has nothing to do with "decaying" - it's exactly the same as if you removed a level of indirection from your example. i.e. int intArray[5] cannot be passed to a function with a parameter int - the function needs to take the whole int array[].

3 Comments

"This has nothing to do with 'decaying'" - it does, and also "Fraction *fractArray[5]. Another way to write that would be Fraction **fractArray" is wrong, arrays aren't pointers.
That so-called decay occurs isn't in dispute, it's just not relevant. Whether you wish to use pointer or array syntax for the function parameter, the result is the same - a pointer is passed. The OPs only problem was they had missed the necessary level of indirection, and was consequently trying to fit an array of Foos into a parameter only specified to take one.
Also, it might be wise to point out that while array syntax - including a specified size - is accepted for function parameters, it truly is treated as a pointer. And pointers don't know anything about how many items they point to. It's safer to just use pointer syntax consistently instead, to avoid thinking that a parameter "int numbers[10]" will have any compile-time checking on the array size - it won't.

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.