From: Robby on 17 Jan 2010 14:09 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
From: Robby on 17 Jan 2010 15:59 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 "Robby" wrote: > 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
From: Igor Tandetnik on 17 Jan 2010 23:33 Robby wrote: > 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: 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
From: Ulrich Eckhardt on 18 Jan 2010 02:45 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); Uli -- C++ FAQ: http://parashift.com/c++-faq-lite Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
From: Robby on 18 Jan 2010 11:36 "Igor Tandetnik" wrote: > 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
|
Next
|
Last
Pages: 1 2 3 Prev: searching a class to use sockets Next: SetMapMode and MouseMove messages |