I am programming in C. I am using also a library to create tasks which can communicate among them via messages. The content of these messages must be the pointer to the data you want to exchange.
In my case I want to send an array so I am sending a pointer to the array. In the receiving task I can then access the different elements by increasing the pointer, but, is there a way to know how long the array was?
Thank you in advance.
No, there is no way to do this as arrays decay to pointers when passed to functions, thus all information regarding size is lost.
You can include another parameter to specify the array length
You can mark the end of the array with a special value (perhaps like
argvdoes)You can put your array into a structure and pass that (and suffer the performance penalties) or pass the a pointer to the structure
If you decide to go the first route, you can use a nice feature of C99, even if it doesn’t actually enforce that
arrhas at leastlenelements: