From: Robby on
"Ulrich Eckhardt" wrote:

> Robby wrote:
> > Okay, I know how to pass an array to a function....
>
> That's interesting, because even the C and C++ standards don't know that!
>
> > int addNumbers(int fiveNumbers[]);
>
> This is equivalent to
>
> int addNumbers(int* fiveNumbers);
>
> You are not passing an array but a badly disguised pointer. Arrays are not
> copyable or assignable, hence can't be passed to or from functions. What
> you can do is pass a reference to an array:
>
> int addNumbers( int (&fiveNumbers)[5]);
>
> Alternatively, and easier to read:
>
> typedef int fiveInts[5];
> addNumbers(fiveInts& fiveNumbers);

Hi Uli,

Thanks for your help! very appreciated!

--
Best regards
Roberto



From: Ulrich Eckhardt on
Igor Tandetnik wrote:
> You could mention array dimensions in the function definition:
>
> int addNumbers(int fiveNumbers[5]) { ... }
>
> This also tells anyone reading the code that the function expects an array
> of exactly 5 elements.

Well, anyone but not anything, in particular not the compiler, which will
neither check nor even warn you if you supply more or less. IOW, this is
also the same as

int addNumbers(int* fiveNumbers);

> Barring that, you can tell the debugger how many
> elements you want to watch by entering "fiveNumbers,5" (without quotes) in
> the Watch window.

Another alternative, tell it to the function:

int addNumbers(int const* numbers, size_t size);

or maybe even:

template<size_t len>
int addNumbers(int numbers[len]);

Of course you can also go STL style and pass in two iterators to the
sequence to add up. Hmmm, isn't there such an algorithm already in the C++
standard lib?

Uli

--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
From: Igor Tandetnik on
Ulrich Eckhardt wrote:
> Igor Tandetnik wrote:
>> You could mention array dimensions in the function definition:
>>
>> int addNumbers(int fiveNumbers[5]) { ... }
>>
>> This also tells anyone reading the code that the function expects an array
>> of exactly 5 elements.
>
> Well, anyone but not anything, in particular not the compiler, which will
> neither check nor even warn you if you supply more or less.

Yes, it's purely for self-documentation. I also hoped the debugger might pick it up and show five elements in the Watch window, but I haven't tested this hypothesis.

> Another alternative, tell it to the function:
>
> int addNumbers(int const* numbers, size_t size);

That wouldn't help with the debugger, would it? The original question was, how to view the array in the Watch window.

> or maybe even:
>
> template<size_t len>
> int addNumbers(int numbers[len]);

It's a C program, not C++.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
From: Barry Schwarz on
On Mon, 18 Jan 2010 08:45:36 +0100, Ulrich Eckhardt
<eckhardt(a)satorlaser.com> wrote:

>Robby wrote:
>> Okay, I know how to pass an array to a function....
>
>That's interesting, because even the C and C++ standards don't know that!
>
>> int addNumbers(int fiveNumbers[]);
>
>This is equivalent to
>
> int addNumbers(int* fiveNumbers);
>
>You are not passing an array but a badly disguised pointer. Arrays are not
>copyable or assignable, hence can't be passed to or from functions. What
>you can do is pass a reference to an array:
>
> int addNumbers( int (&fiveNumbers)[5]);
>
>Alternatively, and easier to read:
>
> typedef int fiveInts[5];
> addNumbers(fiveInts& fiveNumbers);

C does not have references.

--
Remove del for email
From: Ulrich Eckhardt on
Barry Schwarz wrote:
> On Mon, 18 Jan 2010 08:45:36 +0100, Ulrich Eckhardt
> <eckhardt(a)satorlaser.com> wrote:
>>Alternatively, and easier to read:
>>
>> typedef int fiveInts[5];
>> addNumbers(fiveInts& fiveNumbers);
>
> C does not have references.
>

This is about C++ though, ported from C, IIUC. ;)

Uli

--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932