Thread: Array with different data types

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    97

    Array with different data types

    Hi everyone,

    I have a int16_t array that holds some data. Now, I want to add a new element that is type of uint32. How can I added in the array?

    Code:
    int16_t m_array[] = {
          s_accel.x, s_accel.y, s_accel.z, 
          s_gyro.x, s_gyro.y, s_gyro.z, 
          bmm150.data.x, bmm150.data.y, bmm150.data.z,
          ADC_RawData[0], ADC_RawData[1], ADC_RawData[2], ADC_RawData[3],
          ADC_RawData[4], ADC_RawData[5], ADC_RawData[6], ADC_RawData[7]};


    Thanks
    Nick

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    In an array all values must have the same type.
    You could create an array of uint32_t and store uint32_t in it.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    97
    It has to be on the same array since this is payload I have to transmit. The same applyies for signed and unsigned? I am considering to shift bitwise, something like this:

    Code:
    uint32 counter++;
    uint16 MSB =  counter>>16;
    uint16 LSB =  counter;
    Can I have uint16 into an int16 array?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    It looks like you need an array of a struct to hold the information.The struct would have variables of the correct types for your data.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by Nikosant03 View Post
    Hi everyone,

    I have a int16_t array that holds some data. Now, I want to add a new element that is type of uint32. How can I added in the array?

    Code:
    int16_t m_array[] = {
          s_accel.x, s_accel.y, s_accel.z, 
          s_gyro.x, s_gyro.y, s_gyro.z, 
          bmm150.data.x, bmm150.data.y, bmm150.data.z,
          ADC_RawData[0], ADC_RawData[1], ADC_RawData[2], ADC_RawData[3],
          ADC_RawData[4], ADC_RawData[5], ADC_RawData[6], ADC_RawData[7]};


    Thanks
    Nick

    C doesn't allow for mixed type arrays. In C, arrays are always arranged with the elements consecutive in memory. So all elements have to be of the same size. In fact they also have to be of the same type. Other languages like Javascript allow for mixed type arrays, but the underlying data structures are higher level.

    There's no good way to achieve what you want to do. You could use a wider element type, but because the int16_t's are signed whilst the uint32_t's are unsigned, this means going to 64 bits. You could us a union with a "type" field, but that is very awkward and inefficient.

    Best thing is to rewrite the program so data is not held in one array.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > It has to be on the same array since this is payload I have to transmit.
    Transmit where (and how)?

    Network send() for example won't care whether it's one buffer or two, if you decide to send the uint16 in one buffer and the uint32 in another.


    Serialization - Wikipedia
    You normally separate your internal storage representation from your external transmission format.

    While everything was a uint16, everything was peachy.
    The first chink in the armour was the uint32, and then it's going to get messy if you also want to send a string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 03-27-2017, 02:06 AM
  2. typecast array of bytes to larger data types
    By codegrush in forum C Programming
    Replies: 4
    Last Post: 03-20-2012, 11:39 PM
  3. How to declare array with combined data types?
    By 843 in forum C Programming
    Replies: 2
    Last Post: 12-01-2010, 09:44 AM
  4. data types types...
    By gftmc in forum C++ Programming
    Replies: 3
    Last Post: 09-11-2006, 11:30 AM
  5. help "wrapping" data types in char array
    By hampycalc in forum C Programming
    Replies: 5
    Last Post: 03-09-2006, 02:36 PM

Tags for this Thread