In my application I have a number of different structure variables. In all cases the members are unsigned char. I pass the structure to a function via a pointer where the various structures may have a different number of members with different names. Within the function I need to access the members in an indexed form. It is easy enough to use the first member to contain the size of the structure. If I used an array, indexing would be simple, but this would create problems down the line. Arrays aso have their problems - for example, strlen() returns a value of 0XFF if the array is uninitialised, as would be the case for an array that is to be written. I suppose I could always use an array to pass the data to/from the function and transfer between array and structure in the calling function, but that is hardly elegant programming!!!!
OK, but why are you telling us?
If I used an array, indexing would be simple, but this would create problems down the line.
What kind of problems?
Arrays aso have their problems - for example, strlen() returns a value of 0XFF if the array is uninitialised, as would be the case for an array that is to be written.
Well, strlen() isn't supposed to be used on arbitrary arrays - it only works on arrays that actually contain an ASCII string (terminated by a null character, which is how strlen determines the length).
I suppose I could always use an array to pass the data to/from the function and transfer between array and structure in the calling function, but that is hardly elegant programming!!!!
You could also just cast the structure pointer to an array pointer. I don't see why using an array in the first place should be such a big problem, though.
You could also just cast the structure pointer to an array pointer.
Oops. That should be "char pointer".
I agree about strlen(). That's why I could not use it. I want an index because I will be doing a variable-length sequential read/write to an I2C device, and the calling program is more understandable if structures are used. E.G one structure may contain four bytes for time while another structure may contain 10 bytes of operating parameters. For a Real Time Clock I would prefer to use Time.Seconds rather than Time[i]. This whole thing is no big deal - I am simply interested in code quality and readability as well as functionality.
What about a Union?
I thought of that, but a union is essentially a struct that uses the same memory location for all members - so only one member exists at any one time, thereby saving memory. I need all members at all times. A union is useful if you want one variable (a union type) to be able to hold different types of data at different times.
A union is useful if you want one variable (a union type) to be able to hold different types of data at different times.
A union is also helpful for accessing the same data in different ways (e.g. as named members of a structure, and as elements of an array). Be aware that this is machine-specific and code that relies on using a union this way needs to be modified accordingly when compiled on a different architecture.
E.g.
typedef struct { unsigned char length; unsigned char some_data; unsigned char more_data; unsigned char yet_more_data; } my_data_struct; typedef union { my_data_struct as_struct; unsigned char as_array; } my_data_union; ... { my_data_union my_data; ... my_data.as_struct.length = 3; my_data.as_struct.some_data = 0xAB; my_data.as_struct.more_data = 0xDE; my_data.as_struct.yet_more_data = 0xF0; ... peripheral_outpt = my_data.as_array[0]; wait_for_peripheral(); peripheral_outpt = my_data.as_array[1]; wait_for_peripheral(); peripheral_outpt = my_data.as_array[2]; wait_for_peripheral(); peripheral_outpt = my_data.as_array[3]; ... }
Thanks for that, I think I have been given enough ideas to keep me busy for some time.
typedef union { my_data_struct as_struct; unsigned char as_array; } my_data_union;
should be
typedef union { my_data_struct as_struct; unsigned char as_array[4]; } my_data_union;