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?
2 Answers 2
Consider the following:
void printBook(const books &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.
Using Arrays-of-Parts (AoP) to represent a single item or "record" means that record's information is scattered among the AoP. When you need to do more than storing and listing 'N' records, that can get cumbersome and error-prone (as in: you miss one of the AoP when writing one of your routines). It also limits how you can allocate memory for the set of records.
Using an Array-of-Structs (AoS) only requires you to manipulate the one struct as a unit. If you move a record, all of its parts go with it. If you delete it, all of its parts get deleted. When you need more space, you only need to re-declare one array instead each of the individual parts arrays.
And one step up from an AoS is a Linked List of Structs (LLoS). Each record will contains one or more pointers (links) to some other record(s). Sorting then requires nothing more than writing some record's address into this record's link, and you've established a precedence. LLoS really shine when the records get large; writing a couple of pointers is way faster than copying 100 or more bytes. Same with deleting a record (call it "me"): change my predecessor's link-to-me to instead link to my successor, and Bang, done! I'm out of the list, there's no hole left as there would be with AoS or AoP, and my memory can be de-allocated, or I can be linked onto a different list.
Bottom line, arrays can get cumbersome when the data-set is large and and the records are complex. Structs gather a whole record together and allow more flexible manipulation.
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.