Skip to main content

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, this code(written in cC for arduinoArduino)

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
So what is the point?

I have another question:

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

I am a newbie in C and I can not understand the point of using 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?

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?

Source Link
Jack
  • 143
  • 1
  • 2
  • 6

Use of struct and sizeof in C for Arduino

I am a newbie in C and I can not understand the point of using 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?