Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

C

Learn step by step



C-Structures

C arrays allow you to define data type of variables that can hold several data items of the same kind but structure is a user defined data type available in C programming, That allows you to combine data items of different types.

Structures are used to represent a record, For example, if you want to keep track of your books in a library or all student in a school.

Defining a Structure

To define a structure, you must use the struct keyword. The struct keyword defines a new data type, with more than one member. The format of the struct statement is:

struct [structure tag]
{
member definition;
member definition;
...
member definition;
} [one or more structure variables];

The structure tag is optional and each member definition is like a normal variable definition, such as int i; or float f, etc. At the end of the structure's definition, prior to the final semicolon, you can specify structure variables but it is also optional. Here is the way you would declare the Book structure:

struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
Accessing Structure Members

To access any member of a structure, we use the member access operator (.). The member access operator is coded as a period between the structure variable name and the structure member that we wish to access. To define variables of structure type you would use struct keyword. Below given is the example to explain usage of structure:


#include <stdio.h>
#include <string.h>

struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
};

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, "TutOn-Redefining Online education");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 3467543;

/* book 2 specification */
strcpy( Book2.title, "C++ Programming");
strcpy( Book2.author, "TutOn-Redefining Online education");
strcpy( Book2.subject, "C++ Programming Tutorial");
Book2.book_id = 3467567;

/* print Book1 info */
printf( "Book 1 title : %s\n", Book1.title);
printf( "Book 1 author : %s\n", Book1.author);
printf( "Book 1 subject : %s\n", Book1.subject);
printf( "Book 1 book_id : %d\n", Book1.book_id);

/* print Book2 info */
printf( "Book 2 title : %s\n", Book2.title);
printf( "Book 2 author : %s\n", Book2.author);
printf( "Book 2 subject : %s\n", Book2.subject);
printf( "Book 2 book_id : %d\n", Book2.book_id);

return 0;
} RUN

Structures as Function Arguments

You can pass a structure as functions argument in very similar way as you pass any other variable. You would access structure variables in the similar way as you have accessed in the above example:


#include <stdio.h>
#include <string.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, "TutOn-Redefining Online education");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 3467543;

/* book 2 specification */
strcpy( Book2.title, "C++ Programming");
strcpy( Book2.author, "TutOn-Redefining Online education");
strcpy( Book2.subject, "C++ Programming Tutorial");
Book2.book_id = 3467567;

/* 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);
} RUN

Pointers to Structures

You can define pointers to structures in similar way as you define pointer to any variable:

struct Books *struct_pointer;

Now, you can store the address of any structure type variable in the above defined pointer. To find the address of a structure variable, place the &(address of) operator before the structure's name:

struct_pointer = &Book1;

To access the members of a structure using a pointer to that structure, you must use the -> operator as follows:

struct_pointer->title;
Bit Fields

Bit Fields allow the packing of data in a structure. This is useful when memory or data storage is at a premium.

struct packed_struct {
unsigned int f1:1;
unsigned int f2:1;
unsigned int f3:1;
unsigned int f4:1;
unsigned int type:4;
unsigned int my_int:9;
} pack

Here, the packed_struct structure contains 6 members: Four 1 bit flags f1..f3, One 4 bit type and One 9 bit my_int.

C automatically packs the above bit fields as compactly as possible, provided that the max length of the field is less than or equal to the integer word length of the computer. If that is not the case then some compilers may allow memory overlap for fields whilst other would store the next field in the next word.



Related Videos