Thread: Trying to make the following work but getting invalid type conversion?

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    91

    Trying to make the following work but getting invalid type conversion?

    Hi All....
    I am getting a type conversion within the following function call. Can someone explain how I can fix this? I have 4 pieces of info in the struct and would like to use a for loop to assign them rather than one by one but I don't really know how to do it....I would like to do it this way if I can as I am not a SW person by nature so I go back onto my old code a lot to teach myself....Thank you

    Code:
    typedef struct{  uint16_t Vbias;
      uint16_t SiPMCurrent;
      uint16_t LEDCurrent[2];
    } *t_SenseDataRaw;
    
    
    float ADValues[4] = {0};
    t_SenseDataRaw SensorData = {0};
    
    
    void voltage(t_SenseDataRaw *k);
    
    
    void main(){
    	voltage(&SensorData);
    }
    
    
    void voltage(t_SenseDataRaw *k){
      for (uint8_t i = 0; i < 4; i++){
        ADValues[i] = (float)(*(k + i));
      }
      for (uint8_t i = 0; i < 4; i++){
        ADValues[i] = ADValues[i] * 3.3/4096;
      }
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why did you typedef that struct as a pointer? I really don't recommend hiding a pointer inside a typedef.

    Why all the horrible global variables?

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    This doesn't make sense. What do you expect it to do?
    Code:
        ADValues[i] = (float)(*(k + i));
    It is of course equivalent to this:
    Code:
        ADValues[i] = (float)k[i];
    But you can't convert a struct to a float like that.
    Do you mean to convert a specific member of the struct to a float?
    Or are you trying to reinterpret the underlying bytes of the first part of the struct as a float? (And if so, to what purpose?)

    Also, main returns int. Making it void just makes your code non-portable for no good reason.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    Mar 2020
    Posts
    91
    John.....
    My hope is to somehow put each member of the structure into a common array name so that I can then do math on it in a for loop. So to your question....Yes I would like to convert each member of the structure into a float and put it into my array.....Does this make sense??

    I am by no means saying this is the best thing to do....I am a hardware guy trying to learn code....The best way I find is to jump in and try things and I learn along the way....So yes I would like to do what I said above

    Maybe it is not possible??? but I figure it is bcz all the elements of the struct are the same....Maybe I should make the array an unsigned int and then cast??
    Thanks
    Last edited by ridgerunnersjw; 03-20-2022 at 05:11 PM.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    I still don't really understand what you want to do.
    Maybe something like this.
    (Note that I removed the pointer part of the typedef since it's generally a bad idea to hide a pointer (unless perhaps it's opaque), and I also got rid of the globals.)
    Code:
    #include <stdio.h>
    #include <inttypes.h>
     
    typedef struct {
        uint16_t Vbias;
        uint16_t SiPMCurrent;
        uint16_t LEDCurrent[2];
    } t_SenseDataRaw;
     
    void voltage(t_SenseDataRaw *k, float *adv) {
        adv[0] = k->Vbias;
        adv[1] = k->SiPMCurrent;
        adv[2] = k->LEDCurrent[0];
        adv[3] = k->LEDCurrent[1];
        for (int i = 0; i < 4; i++)
            adv[i] = adv[i] * 3.3 / 4096;
    }
     
    int main() {
        t_SenseDataRaw SensorData;
        SensorData.Vbias = 1;
        SensorData.SiPMCurrent = 2;
        SensorData.LEDCurrent[0] = 3;
        SensorData.LEDCurrent[1] = 4;
     
        float ADValues[4];
        voltage(&SensorData, ADValues);
        for (int i = 0; i < 4; i++)
            printf("%f\n", ADValues[i]);
     
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #6
    Registered User
    Join Date
    Mar 2020
    Posts
    91
    Yea that is where I ended up.....Was hoping that within voltage function there was a way to do the assignment in a loop rather than line by line assigning....idk maybe that is not possible?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If the struct members really are the same type (a uint16_t) and represent the same information (a voltage), then you could do this.
    Code:
    void voltage(t_SenseDataRaw *k, float *adv) {
        uint16_t p = &k->Vbias;  // the first member
        for (int i = 0; i < 4; i++)
            adv[i] = p[i] * 3.3 / 4096;
    }
    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.

  8. #8
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Salem View Post
    If the struct members really are the same type (a uint16_t) and represent the same information (a voltage), then you could do this.
    A small correction:

    Code:
    void voltage(t_SenseDataRaw *k, float *adv) {
        uint16_t *p = &k->Vbias;  // the first member (A pointer!).
        for (int i = 0; i < 4; i++)
            adv[i] = p[i] * 3.3 / 4096;
    }
    And since using pointer aliasing it could be:
    Code:
      uint16_t *p = (uint16_t *)k;
    []s
    Fred

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid conversion What's this?
    By kyel_dai92 in forum C Programming
    Replies: 6
    Last Post: 04-06-2011, 08:44 AM
  2. error: invalid conversion from 'int' to 'int*'
    By csharp100 in forum C++ Programming
    Replies: 4
    Last Post: 02-22-2011, 07:56 AM
  3. invalid conversion from void* to int*
    By Tropod in forum C++ Programming
    Replies: 10
    Last Post: 01-04-2009, 04:15 PM
  4. Invalid Conversion
    By cweb255 in forum Windows Programming
    Replies: 2
    Last Post: 05-28-2005, 07:04 AM
  5. invalid conversion
    By black_sol in forum C++ Programming
    Replies: 3
    Last Post: 05-16-2004, 05:42 PM

Tags for this Thread