1

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?

6
  • It is all about reduction of complexity and abstraction. A book (a struct in your example) has a title, author and identity. Collecting these together instead of scattering them helps when the size of the program grows, and if it "lives" for a long time and needs updating. struct is easy, wait until you get to class :) Commented Dec 7, 2016 at 21:17
  • @Mikael Patel Thank you, I thought struct and class were the same!
    – Jack
    Commented Dec 7, 2016 at 21:18
  • C has no classes. In C++, struct and class are almost the same. The only difference is members being by default private in a class and public in a struct. A C++ struct with no methods (only data members) nor static members is very similar to a C struct. Commented Dec 7, 2016 at 21:23
  • With classes we approach programming from the point of view of operations on local data (objects). In your example above; the class Book could be used in a collection in a class Library. The Library would have member functions such as lookupBookBy(title/name/identity), borrowBook, returnBook(), etc. Commented Dec 7, 2016 at 21:36
  • I have not defined it anywhere. - yes you have, in your code.
    – Nick Gammon
    Commented Dec 8, 2016 at 8:13

2 Answers 2

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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.