Use of struct and sizeof in C for Arduino I am a newbie in C and I can not understand the point of using a structure in C. Could someone explain to me what is the point of defining a structure in C programming?

For example, this code(written in C for Arduino)

struct books
{ 
  char name[30];
  char author[30];
  int ID;
} Mybook, Yourbook;

void setup() 
{
  Serial.begin(9600);
  // put your setup code here, to run once:
  strcpy(Mybook.name, "Girl in the train");

  strcpy(Yourbook.name, "Gone"); 

  Serial.print("Name of the book is:");
  Serial.println(Mybook.name);
  Serial.println((long)&amp;(Mybook.name));
  Serial.println((long)&amp;(Mybook));
  Serial.println((long)&amp;Mybook);
  Serial.println(sizeof(Mybook));

  Serial.print("Name of the book is:");
  Serial.println(Yourbook.name);
  Serial.println((long)&amp;Yourbook);
  Serial.println(sizeof(Yourbook));
}


Would use the same amount of memory as defining

char Mybookname[30];
char Yourbookname[30];


And you should type the same amount of lines.
So what is the point?

I have another question:

Why I get 62 for the sizeof(Mybook)? I have not defined it anywhere. Is it set automatically?
Consider the following:

void printBook(const books &amp;theBook)
{
    Serial.println("Name of the book is: ");
    Serial.println(theBook.name);
    Serial.println("author is: ");
    Serial.println(theBook.author);
    Serial.println("ID is: ");
    Serial.println(theBook.ID);
}

void loop()
{
    printBook(MyBook);
    printBook(YourBook);
}


You should appreciate this avoids the code duplication you have in your
example.


  Why I get 62 for the sizeof(Mybook)?



30 bytes for name[30] (each char is one byte)
30 bytes for author[30]
2 bytes for ID (the AVRs have 16-bit ints)


No padding between the members because this architecture has no
alignment constraints.