Struct in C
Struct?
struct is structures, provide a way of storing many different values with the same group or name. we used struct when we have a lot of data that need to be group for instance. struct contains information about the data.
Defining Struct?
to define structure, you must used struct statement. the syntax for struct is:
struct student {
char name [100];
int age;
int height;
int weight;
};
Accessing Struct members?
you can access any member of the structures, and commonly we used access operator (.). to make it clear, you can see the full example how to use struct:
#include <stdio.h>
struct students {
char name[100];
int age;
int weight;
int height;
};
int main () {
struct students student1; //declare student1 of the student
struct students student2; //declare student2 of the student
//access the information about student1
strcpy(student1.name, "Serafin William");
student1.age = 18;
student1.weight = 52;
student1.height = 170;
//access the information about student2
strcpy(student2.name, "Winner2nd");
student2.age = 17;
student2.weight = 56;
student2.height = 170;
//print the information about student 1
printf("Student 1 name: %s\n",student1.name);
printf("Student 1 age: %d\n",student1.age);
printf("Student 1 name: %d kg\n",student1.weight);
printf("Student 1 name: %d cm\n",student1.height);
//print the information about student2
printf("Student 2 name: %s\n",student2.name);
printf("Student 2 age: %d\n",student2.age);
printf("Student 2 name: %d kg\n",student2.weight);
printf("Student 2 name: %d cm\n",student2.height);
return 0;
}
Another example, How to use in function?
#include <stdio.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
/* function declaration */
void printBook( struct Books book );
int main( ) {
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
/* print Book1 info */
printBook( Book1 );
/* Print Book2 info */
printBook( Book2 );
return 0;
}
void printBook( struct Books book ) {
printf( "Book title : %s\n", book.title);
printf( "Book author : %s\n", book.author);
printf( "Book subject : %s\n", book.subject);
printf( "Book book_id : %d\n", book.book_id);
}
How to use structure in pointer?
#include <stdio.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
/* function declaration */
void printBook( struct Books *book );
int main( ) {
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
/* print Book1 info by passing address of Book1 */
printBook( &Book1 );
/* print Book2 info by passing address of Book2 */
printBook( &Book2 );
return 0;
}
void printBook( struct Books *book ) {
printf( "Book title : %s\n", book->title);
printf( "Book author : %s\n", book->author);
printf( "Book subject : %s\n", book->subject);
printf( "Book book_id : %d\n", book->book_id);
}
References?
#include <stdio.h>
struct someStruct {
unsigned int total;
};
int test(struct someStruct* state) {
state->total = 4;
}
int main () {
struct someStruct s;
s.total = 5;
test(&s);
printf("s.total = %d\n", s.total);
}