Discussion:
"PORTING C" > Viewing an array in wathc window fails!
(too old to reply)
Robby
2010-01-17 19:09:01 UTC
Permalink
Hello,

Okay, I know how to pass an array to a function.... but then not being able
to view the array's contents is not normal.

Please view the sample code at:

http://irc.essex.ac.uk/www.iota-six.co.uk/c/f3_passing_arrays_to_functions.asp

Now, I have modified the code a little and entered it this way... please
view the following code sample:

=================================
#include <stdio.h>

int addNumbers(int fiveNumbers[]);

int main()
{
int y;
int array[5]={1,2,3,4,5};
y = addNumbers(array);
return 0;
}

int addNumbers(int fiveNumbers[])
{
int sum = 0;
int i;

for(i=0 ; i<5 ; i++) {
sum+=fiveNumbers[i];
}
return sum; //<<< BREAKPOINT HERE
}
=============================

When I stop at the breakpoint in the addNumbers() function, the value of sum
does equal 15, which means that the compiler is reading off *all* the correct
values from the fiveNumbers array. But why is it that we can't view the
contents of "fiveNumbers" array in the watch window like this:

fiveNumbers
[0] ... 1
[1] ... 2
[2] ... 3
[3] ... 4
[4] ... 5

All I read in the watch window is:

fiveNumbers
[] ... 1
--
Best regards
Roberto
Robby
2010-01-17 20:59:01 UTC
Permalink
and!

Althought this is a tangeble explanation:

http://www.coolinterview.com/interview/4361/

Is it still normal that compilers would not be able to display all of the
contents (in the watch window) of an array passed into a function.

All help appreciated!
--
Best regards
Roberto
Post by Robby
Hello,
Okay, I know how to pass an array to a function.... but then not being able
to view the array's contents is not normal.
http://irc.essex.ac.uk/www.iota-six.co.uk/c/f3_passing_arrays_to_functions.asp
Now, I have modified the code a little and entered it this way... please
=================================
#include <stdio.h>
int addNumbers(int fiveNumbers[]);
int main()
{
int y;
int array[5]={1,2,3,4,5};
y = addNumbers(array);
return 0;
}
int addNumbers(int fiveNumbers[])
{
int sum = 0;
int i;
for(i=0 ; i<5 ; i++) {
sum+=fiveNumbers[i];
}
return sum; //<<< BREAKPOINT HERE
}
=============================
When I stop at the breakpoint in the addNumbers() function, the value of sum
does equal 15, which means that the compiler is reading off *all* the correct
values from the fiveNumbers array. But why is it that we can't view the
fiveNumbers
[0] ... 1
[1] ... 2
[2] ... 3
[3] ... 4
[4] ... 5
fiveNumbers
[] ... 1
--
Best regards
Roberto
Igor Tandetnik
2010-01-18 04:33:57 UTC
Permalink
Post by Robby
int addNumbers(int fiveNumbers[])
{
int sum = 0;
int i;
for(i=0 ; i<5 ; i++) {
sum+=fiveNumbers[i];
}
return sum; //<<< BREAKPOINT HERE
}
=============================
When I stop at the breakpoint in the addNumbers() function, the value of sum
does equal 15, which means that the compiler is reading off *all* the correct
values from the fiveNumbers array. But why is it that we can't view the
The debugger has no way to know that fiveNumbers array contains five elements. After all, this would work just as well:

int array[10]={1,2,3,4,5,6,7,8,9,10};
y = addNumbers(array);

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. Barring that, you can tell the debugger how many elements you want to watch by entering "fiveNumbers,5" (without quotes) in the Watch window.
--
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
Robby
2010-01-18 16:36:01 UTC
Permalink
This also tells anyone reading the code that the function expects an array of >exactly 5 elements.
I see your point !
Barring that, you can tell the debugger how many elements you want to watch by >entering "fiveNumbers,5" (without quotes) in the Watch window.
That's great ! It works for VC but it does not work for MPLAB.

To resolve this for MPLAB/C32 compiler, I will have to post this one in
another forum.

In a worst case scenario if I really want to see the contents of the array
as the array's data gets manipulated *in* the function (for MPLAB compiler),
the only way out here is to assign the contents of the fiveNumbers[] array to
a locally declared array (local_array) and assign the latter to the watch
window.

Please note, I didn't try the following code!
=================================
#include <stdio.h>

int addNumbers(int fiveNumbers[]);

int main()
{
int y;
int array[5]={1,2,3,4,5};
y = addNumbers(array);
return 0;
}

int addNumbers(int fiveNumbers[])
{
int sum = 0; i;
int local_array[5]; //<< local array assigned to watch window!

// Assign fiveNumbers array to local_array so its data can be traced in
watch
// window
for(i=0;i<5;i++)
local_array[i] = fiveNumbers[i];

// Do some modifications to the array according to its data
if(local_array[2] > 1)
local_array[2] = 200;

// Apply logic ... and so forth!!!!
for(i=0 ; i<5 ; i++) {
sum+=local_array[i];
}
return sum;
}
=============================

It absolutely can happen that the above code can be more complex in
manipulating the array's data bywhich one might really need to see any data
developent in the array ... but this one is just a simple example where I may
want to *watch* the value of local_array[2] get modified as I tests the code.

Its too bad that I can't get the "fiveNumbers,5" way to work in MPLAB/C32
compiler.... I would of very much so prefered it instead of copying arrays
around!

Thanks for your help Igor!

Regards
Rob
Ulrich Eckhardt
2010-01-19 08:13:19 UTC
Permalink
Post by Igor Tandetnik
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);
Post by Igor Tandetnik
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
Igor Tandetnik
2010-01-19 13:14:20 UTC
Permalink
Post by Ulrich Eckhardt
Post by Igor Tandetnik
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.
Post by Ulrich Eckhardt
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.
Post by Ulrich Eckhardt
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
Ulrich Eckhardt
2010-01-18 07:45:36 UTC
Permalink
Post by Robby
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!
Post by Robby
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);

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

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Robby
2010-01-18 17:12:01 UTC
Permalink
Post by Ulrich Eckhardt
Post by Robby
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!
Post by Robby
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
int addNumbers( int (&fiveNumbers)[5]);
typedef int fiveInts[5];
addNumbers(fiveInts& fiveNumbers);
Hi Uli,

Thanks for your help! very appreciated!
--
Best regards
Roberto
Barry Schwarz
2010-01-31 00:53:25 UTC
Permalink
On Mon, 18 Jan 2010 08:45:36 +0100, Ulrich Eckhardt
Post by Ulrich Eckhardt
Post by Robby
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!
Post by Robby
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
int addNumbers( int (&fiveNumbers)[5]);
typedef int fiveInts[5];
addNumbers(fiveInts& fiveNumbers);
C does not have references.
--
Remove del for email
Ulrich Eckhardt
2010-02-01 08:29:09 UTC
Permalink
Post by Barry Schwarz
On Mon, 18 Jan 2010 08:45:36 +0100, Ulrich Eckhardt
Post by Ulrich Eckhardt
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
Barry Schwarz
2010-02-01 10:46:45 UTC
Permalink
On Mon, 01 Feb 2010 09:29:09 +0100, Ulrich Eckhardt
Post by Ulrich Eckhardt
Post by Barry Schwarz
On Mon, 18 Jan 2010 08:45:36 +0100, Ulrich Eckhardt
Post by Ulrich Eckhardt
typedef int fiveInts[5];
addNumbers(fiveInts& fiveNumbers);
C does not have references.
This is about C++ though, ported from C, IIUC. ;)
He appears to be porting to MPLAB/C32. He continues to post here even
though almost none of his questions are related to anything specific
to this Microsoft product.
--
Remove del for email
Loading...