Search code examples
cstructoperator-keyword

What does this operator mean in C?


I've the following code snippet:

for(int i=0;i<6; i++)  rtk->rb[i]=i<3?opt->rb[i]:0.0;

I can't understand the meaning of this line...rtk is a struct that has a field named rb (which is an array) and opt is another struct, which also has a the same field rb... the simbols < and ? are confusing me... rtk type is rtk_t, while opt type is prcopt_t:

typedef struct {        
    
    double rb[6];       /* base position/velocity (ecef) (m|m/s) */
    ....
} rtk_t;

typedef struct {        

    double rb[3];       /* base position for relative mode {x,y,z} (ecef) (m) */
    
} prcopt_t;

Solution

  • the right part of this here :

    rtk->rb[i]=i<3?opt->rb[i]:0.0;
    

    i.e.

    i<3?opt->rb[i]:0.0
    

    is another way to say:

     if (i < 3)
     {
         x = opt->rb[i];
     }
     else
     {
         x = 0.0;
     }
    

    only that the way you wrote it is using ternary operators