-1

I am currently studying the crazyflie 2.0 drone Firmware. For those who do not know the drone, here is a link to the Website:https://www.bitcraze.io/crazyflie-2/ It is an open source Project.

Anyway, I have toruble understanding some part of the Firmware Code. Actually it might be very simple, but I am very new to programming in C.

struct CommanderCrtpValues
{
  float roll;
  float pitch;
  float yaw;
  uint16_t thrust;
} __attribute__((packed));

static struct CommanderCrtpValues targetVal[2];

You can find this Piece of Code at: https://github.com/bitcraze crazyflie-firmware/modules/src/commander.c

I do not understand the last line. I believe that it is an assignment of a struct to an Array, named targetVal, but I am not sure. Could you explain what's really going on ?

henry
  • 875
  • 1
  • 18
  • 48
  • 3
    It's an array. It contains two elements, each element is a struct of the type shown immediately above it. – enhzflep Mar 26 '16 at 08:21

2 Answers2

3

This is creating a static array of 2 CommanderCrtpValues structures.

Since this is declared in the global scope its memory is going to be initialized as 0 (i.e. the all the fields will have the value 0) when the program starts, see Why are global variables always initialized to '0', but not local variables?. I do not think this detail is important in this particular case but it is used in other places of the firmware.

(Disclaimer: I am the author of this code.)

Community
  • 1
  • 1
  • Welcome to Stack Overflow! As a new member, you may want to read the short introductory [tour] to familiarize yourself with the basics. I've taken the liberty to edit some minor points – answers are supposed to be able to stand on their own. It is sometimes useful to refer to others' posts but in this case it seems not necessary. I have also removed the remark about commenting. – Jongware Mar 29 '16 at 09:59
0

The code defines a structure named CommanderCrtpValues that has four fields (roll, pitch, yaw, thrust).

The last line defines a variable named targetVal that is an array of 2 elements. Each array element is a structure of CommanderCrtpValues.

The variable targetVal has not been initialized yet so the actual content of each element is undefined. Even though you know that they are CommanderCrtpValues structures, you don't know what values the fields contain.

T Johnson
  • 824
  • 1
  • 5
  • 10