简体   繁体   中英

How do you call a function with an array of structs?

I have the following LCD driver code, and I am unsure how to call this function

typedef struct  {
  int16_t X;
  int16_t Y;
} Point, * pPoint;

void LCD_PolyLine(pPoint Points, uint16_t PointCount) {
  int16_t X = 0, Y = 0;
  while(--PointCount) {
    X = Points->X;
    Y = Points->Y;
    Points++;
    LCD_DrawUniLine(X, Y, Points->X, Points->Y);
  }
}

It doesn't make sense to have the first argument of the function be "pPoint Points". To me it seems like it should be "pPoint *Points". I could then create an array of pPoints and pass this address to it.

How else would you call this driver function without modifying it?

It doesn't make sense to have the first argument of the function be pPoint Points .

It actually makes perfect sense, because pPoint is defined as Point* , ie a pointer to Point . It is equivalent to

void LCD_PolyLine(Point *Points, uint16_t PointCount)

which is the correct signature. All you need to do is to pass an array of struct Point , followed by the count of elements in it.

On the last line of the the Point struct and typedef definition, after the comma it says * pPoint . This is notation that defines the type pPoint as a Point * . Note the "p" on pPoint is common notation for a pointer type. So you are right, you can just normally create an array of Point structs and pass the pointer to the function. (Just be careful not to pass the address of the pointer, which would be a Point ** .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM