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)&(Mybook.name));
Serial.println((long)&(Mybook));
Serial.println((long)&Mybook);
Serial.println(sizeof(Mybook));
Serial.print("Name of the book is:");
Serial.println(Yourbook.name);
Serial.println((long)&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?
private
in a class andpublic
in a struct. A C++ struct with no methods (only data members) norstatic
members is very similar to a C struct.