Hi, I am new to C and am trying to write a function which accepts no input parameters, but returns either a byte array or two separate byte variables, whatever will work.
Thanks for the code, I slowly start to understand the concept of pointers. My gcc-4.2 accepted the code posted by nkcelectronics but gave a warning (incompatible pointer type) during compiling. The program worked well despite the warning.
The Arduino-environment though didn't compile the code nkcelectronics suggested and aborted with an error (cannot convert pointer type), so I had to change the function-call to «getdat(&data[0])» and then it worked perfectly. It seems, as if you have to specificly hand over the address of the first element of the array.
Either way, the same address gets passed to getdata(). In the function declaration for getdat(), I would use 'byte pdata[]' rather than 'byte *pdata' because I will be referencing pdata as an array rather than a character pointer. Once again, the way you did it was equivalent, but saying it's an array makes it a tiny bit more self-documenting.
By the way, if you wanted to change the value of two variables in getdat(), rather than using an array, here's one way to do it:
My gcc-4.2 accepted the code posted by nkcelectronics but gave a warning (incompatible pointer type) during compiling. The program worked well despite the warning.
In C an array IS a pointer. You should be calling getdat(data) rather than getdat(&data);. The latter will probably "work" but will overwrite some random place in memory which will not be where you intended.
("data" is equivalent to "&data[0]", i.e. the address of the start of the array. "&data" is the address of the address of the start of the array)
a) If your example shows ALL you want to do, you could be really, really "bad" and just use some global variables.
b) If you want one function to return two (or more) numbers to one of a limited number of destinations, you could still use global variables ("bad"), but pass a "which destination" switch to the function....