Solved: Problem with format string

Hi,
for the following sketch

#define CENTERX 744
#define CENTERY 115
const int arrow_x[] = { -5,  5,  0,  16, 26,-21,  35, 43,-39};
const int arrow_y[] = {-55,-55, 55, -53,-49, 51, -43,-35, 39};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  for (int i=0; i<3; i++) {
    int adrx=(int)arrow_x + i*3, adry=(int)arrow_y + i*3;
    char ostr[60];
    sprintf(ostr,"dir=%2d: x=[%i %i %i]\n        y=[%i %i %i]",
        i,
        (int)(*(adrx))+CENTERX,(int)(*(adrx+1))+CENTERX,(int)(*(adrx+2))+CENTERX,
        (int)(*(adry))+CENTERY,(int)(*(adry+1))+CENTERY,(int)(*(adrx+2))+CENTERY);
    Serial.println(ostr);
  }  
}

void loop() {
  // put your main code here, to run repeatedly:

}

I get errors from compiler

Arduino: 1.8.15 (Windows 10), TD: 1.57, Board: "Teensy 3.2 / 3.1, Serial, 72 MHz, Faster, German"

sketch_nov02a: In function 'void setup()':

sketch_nov02a:14: error: invalid type argument of unary '*' (have 'int')

         (int)(*(adrx))+CENTERX,(int)(*(adrx+1))+CENTERX,(int)(*(adrx+2))+CENTERX,

                     ^

sketch_nov02a:14: error: 'CENTERX' was not declared in this scope

         (int)(*(adrx))+CENTERX,(int)(*(adrx+1))+CENTERX,(int)(*(adrx+2))+CENTERX,

                        ^

sketch_nov02a:14: error: invalid type argument of unary '*' (have 'int')

         (int)(*(adrx))+CENTERX,(int)(*(adrx+1))+CENTERX,(int)(*(adrx+2))+CENTERX,

                                              ^

sketch_nov02a:14: error: invalid type argument of unary '*' (have 'int')

         (int)(*(adrx))+CENTERX,(int)(*(adrx+1))+CENTERX,(int)(*(adrx+2))+CENTERX,

                                                                       ^

sketch_nov02a:15: error: invalid type argument of unary '*' (have 'int')

         (int)(*(adry))+CENTERY,(int)(*(adry+1))+CENTERY,(int)(*(adrx+2))+CENTERY);

                     ^

sketch_nov02a:15: error: 'CENTERY' was not declared in this scope

         (int)(*(adry))+CENTERY,(int)(*(adry+1))+CENTERY,(int)(*(adrx+2))+CENTERY);

                        ^

sketch_nov02a:15: error: invalid type argument of unary '*' (have 'int')

         (int)(*(adry))+CENTERY,(int)(*(adry+1))+CENTERY,(int)(*(adrx+2))+CENTERY);

                                              ^

sketch_nov02a:15: error: invalid type argument of unary '*' (have 'int')

         (int)(*(adry))+CENTERY,(int)(*(adry+1))+CENTERY,(int)(*(adrx+2))+CENTERY);

                                                                       ^

invalid type argument of unary '*' (have 'int')

What is wrong with the code?

Where are CENTERX and CENTERY defined? I don't see that anywhere in your code.

Could you explain what is (*(adrx)) :

As far I see, you have a problem not with format string, you have a problem with correct understanding of arrays and pointers....

Is not a error, but why do you wrote two expressions with comma?

is it to make the code less comprehensible and more likely to put errors in it?

To make matters worse, arrow_x is an array of int, and you're casting the array (which "decays" to a pointer to int) to int. That probably doesn't give you the results you expect. Casts should be used only where needed, not to simply get rid of compile errors like "invalid conversion ...".

1 Like

I corrected my initial posting, defining CENTERX and CENTERY.

The initial code within the for loop was

...
sprintf(ostr,"dir=%2d: x=[%i %i %i]\n        y=[%i %i %i]",
        i,
        (int)(*(arrow_x + i*3))+CENTERX,(int)(*(arrow_x + i*3+1))+CENTERX,(int)(*(arrow_x + i*3+2))+CENTERX,
        (int)(*(arrow_y + i*3))+CENTERY,(int)(*(arrow_y + i*3+1))+CENTERY,(int)(*(arrow_y + i*3+2))+CENTERY);
...

I wanted to make it simpler by replacing arrow_x + i3 with a variable adrx and arrow_y + i3 with a variable adry.
But here I got lost.

Is the problem solved?

yes, it is solved :wink:

Please use "Solution" mark in the most useful comment.

This is another way of saying arrow_x[i*3], which is simpler and clearer, IMO. So you can simplify your sprintf call to this:

sprintf(ostr,"dir=%2d: x=[%i %i %i]\n        y=[%i %i %i]",
        i,
        arrow_x[i*3]+CENTERX,arrow_x[i*3+1]+CENTERX,arrow_x[i*3+2]+CENTERX,
        arrow_y[i*3]+CENTERY,arrow_y[i*3+1]+CENTERY,arrow_y[i*3+2]+CENTERY);

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.