|
Page 1 of 1
|
[ 8 posts ] |
|
2 questions regarding arrays
| Author |
Message |
|
yfnguitar
Rookie
Joined: Fri Mar 02, 2007 11:41 pm Posts: 5 Location: Pennsylvania
|
 2 questions regarding arrays
1. Is it possible to pass an array to a function?
for example:
I know the above code will not work, but how would one go about passing all of the values of a predefined array (or their memory locations) into the function? Is there a way to pass by reference (with pointers)? 2. Are there any dynamic memory types supported (like an ArrayList object in Java) that would enable me to refer to a group of homogeneous data by number that is defined at run-time instead of compile-time (like an array). (The number of elements in the list is declared at run-time by a variable, int x, for example.) I want to be able to pass an array (or similar data structure) of bytes, the length of which is unknown until run-time, to a function and be able to retreive data from the data structure inside the function. I am trying to pass an array of bytes to a function that sends them via I2C with the formatting (such as the length of the message, I2CAddress, and internal register to write to) added to the front.  |  |  |  | Code: bool sendI2CMessage(tSensors nPortIndex, const int kI2CAddress, const int kRegisterIndex, int nReplyLength, byte nMsgData[], const int nMsgDataLength) { byte nMsg[nMsgDataLength + 3]; nMsg[0] = 2 + nMsgDataLength; nMsg[1] = kI2CAddress; nMsg[2] = kRegisterIndex;
for(int i = 0; i < nMsgDataLength; i++) { nMsg[3 + i] = nMsgData[i]; }
if (nI2CStatus[nPortIndex] == STAT_COMM_PENDING) return false; // Can't send message to a busy device
sendI2CMsg(nPortIndex, nMsg[0], nReplyLength); return true; }
task main() {
byte myData[5];
bool success = sendI2CMessage(S1, 0x02, 0x41, 0, myData, 5);
}
|  |  |  |  |
The above code generates multiple errors:
1. Since in the declaration of sendI2CMessage(), byte nMsgData[] is not the right way to pass an array. Alternatively, replacing it with byte nMessageData would result in only the first element of the array being passed into the function.
2. Since the byte nMsg[nMsgDataLength + 3]; array declaration expects a constant, even though nMsgDataLength is defined as a constant and is added to a constant, 3.
I'm sorry this is so repetitive/confusing. I am thoroughly confused.
Thanks for your help.
yfnguitar
|
| Sat Dec 01, 2007 10:34 pm |
|
 |
|
elemes
Expert
Joined: Fri Nov 09, 2007 4:51 am Posts: 121 Location: Hungary, Europe
|
 Arrays
Hi,
It shall be possible to pass an array to a function but don't know how to declare that function (and use the array parameters).
E.g. sendI2cmessage is a function that expects a byte array as parameter. declaration of this function is: The funny part is, that -- even if I am able to declare a function that expects a "pointer" to a value as a parameter -- I don't know how to access further elements of the array that has been passed on to that function. I have run through the coding examples but for the first sight I have not found such code parts.
Laszlo
|
| Tue Dec 04, 2007 6:14 am |
|
 |
|
mcrosbie
Rookie
Joined: Sun Jul 22, 2007 6:32 pm Posts: 16 Location: Ireland
|
 Wrap the array in a struct
The solution I use is to wrap the array in a struct and pass the struct in as a parameter to the function.
Regards,
Mark
|
| Tue Dec 04, 2007 4:37 pm |
|
 |
|
adam_naila2
Rookie
Joined: Wed Nov 14, 2007 4:59 pm Posts: 11
|
Hello,
Regarding the last reply, stating that the solution is to pass a struct to the function (lets call it functionA), is it possible to pass the struct into a function called within funtionA?
i.e. the equivalent of a pointer to a pointer, but passing by reference.
Attempting this I get the following warning:
" *Warning*:Invalid '=' operation for types '<STRUCT TYPE> &' and '<STRUCT TYPE> &' "
If anyone has any ideas, I would be appreciate it.
_________________ Adam Naila,
Computing, Electronics and Mathematics,
University of West of England.
|
| Wed Dec 05, 2007 5:39 pm |
|
 |
|
elemes
Expert
Joined: Fri Nov 09, 2007 4:51 am Posts: 121 Location: Hungary, Europe
|
 passing array as struct
As mcrosbie proposed I have compiled a short example (I do not have the brick with me but the generated assembly code can be ok).
[/b]
|
| Thu Dec 06, 2007 6:25 am |
|
 |
|
adam_naila2
Rookie
Joined: Wed Nov 14, 2007 4:59 pm Posts: 11
|
Thanks elemes,
I was not clear in my last post that the structure variables need to be updated in the 2nd function, in this case proc2.
There is no difference between the parameter RobotC expects to see, and the parameter which is being passed, with the following compilation warning message:
*Warning*:Invalid '=' operation for types 'POSITION_DETAILS &' and 'POSITION_DETAILS &'
Do you know how to update the variables within the 2nd function?
Thanks, Adam.
|
| Thu Dec 06, 2007 1:22 pm |
|
 |
|
elemes
Expert
Joined: Fri Nov 09, 2007 4:51 am Posts: 121 Location: Hungary, Europe
|
 modifying array elements
I'm not sure if I understand well. I have just changed the proc2 in the previous example on the following way:
I have got neither errors nor warnings and although I have not yet tested the operation on the brick the resulting bytecode suggests that the struct was passed to the subsequent procedures by its address and when proc2 changes x.arr[ 3 ] it would affect the original struct in the main().
|
| Thu Dec 06, 2007 3:53 pm |
|
 |
|
elemes
Expert
Joined: Fri Nov 09, 2007 4:51 am Posts: 121 Location: Hungary, Europe
|
 passing parameters by reference
Just a note: when I learned C back in the 80's from the popular Kernighan-Ritchie book (there was nothing like ++ back then  )) it was clearly stated that function parameters are always passed by VALUE.
If programmer was intended to change the value of the parameters he had to pass the address of the variable (a pointer) to the function. In later implementations of C the parameter passing by REFERENCE appeared.
When a parameter name is preceded with & like in this example the compiler will do all the messy pointer arithmetics that was previously explicitly written by the programmers.
RobotC declares that it has no (full) pointer aritmethics, I assume it is not possible to declare a function that will return a pointer to an array containing pointers to functions returning pointers to integer arrays.
|
| Thu Dec 06, 2007 4:02 pm |
|
|
|
Page 1 of 1
|
[ 8 posts ] |
|
Who is online |
Users browsing this forum: No registered users and 2 guests |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot post attachments in this forum
|
|