This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

array with names

hello to all of you,
is there a way to have a array and give each element of the array a special name?
An example:

float my_array[20];
.
.
for(i=0;i<20;i++)
send(my_array[i];
.
.
and each element of the array should have its special name like
1. actual_temperature
2. minimum_temperature
3. maximal_temperature
but the should share the same memory adress.

Sorry dont tell me Please read the manual, just give me a helpful hint.
Sincerly yours

Parents
  • Hallo to everybody. Now being back from camping-holiday in Italia i wana thank you for your help.

    #define actual_temperature  my_array[1]
    #define minimum_temperature my_array[2]
    #define maximal_temperature my_array[3]
    

    this fits perfect to my requirements, thank you Dan, i would have send you a picture-postcard from Pisa if i have had your adress ;-)

    This hint is very helpful, everybody needs a little help if there are tomato-slices on front of your eyes ;-)

Reply
  • Hallo to everybody. Now being back from camping-holiday in Italia i wana thank you for your help.

    #define actual_temperature  my_array[1]
    #define minimum_temperature my_array[2]
    #define maximal_temperature my_array[3]
    

    this fits perfect to my requirements, thank you Dan, i would have send you a picture-postcard from Pisa if i have had your adress ;-)

    This hint is very helpful, everybody needs a little help if there are tomato-slices on front of your eyes ;-)

Children
  • this fits perfect to my requirements,

    It is a fairly ugly way to do this and will confuse the heck out of anyone trying to maintain it.

    Use a union instead. At least this makes it clear which array you're actually referencing to.

  • this fits perfect to my requirements

    Then why not just declare these three variables instead of an array?

    Or make them fields in a struct, so you can send the entire block of three variables with one call to the send() routine.

  • this fits perfect to my requirements,

    It quite definitely doesn't --- it clearly fails the "have the same address" criterion. And neither is it perfect, in any useful interpretation of the word. A struct would be a whole lot more readable.

  • #define actual_temperature my_array[1]
    #define minimum_temperature my_array[2]
    #define maximal_temperature my_array[3]

    hides the fact that it is an array wheras
    #define MY_ARR_actual_temperature 1
    #define MY_ARR_minimum_temperature 2
    #define MY_ARR_maximal_temperature 3

    and accessing it by my_array[MY_ARR_actual_temperature]
    would not

    I consider it important that an 'array indicator' e.g. MY_ARR_ be included in the offset name, I have found horrendous bugs where the offset from your_array was used on my_array creating a very tough bug (nobody could 'see' it). e.g. if no 'array indicator' was included, could you spot your_array[actual_temperature] in a reasonable time?

    Erik