sturcture
sturcture

 

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

C Structure is also collection of different data types which are grouped together and each element in a structure is called member of c language programming.

If you want to access structure members and structure variable should be declared.

Many structure variables can be created for same structure and memory will be allocated for each separately in c language .

In c language structure is a best practice to initialize a structure to null while declaring, if we don’t assign any values to structure members.

simple different between variable , array and structure are a normal C variable can hold only one data of one data type at a time. An array can hold group of data of same data type. A structure can hold group of data of different data types.

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

Syntax:

Structure tag_name
{
data type var_nm1;
data type var_nm2;
data type var_nm3;
};

The structure tag is optional and each member definition is a normal variable definition, such as int i; or float f; or any other valid variable definition. At the end of the structure’s definition, before the final semicolon, you can specify one or more structure variables but it is optional.

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. You would use struct keyword to define variables of structure type. Following is the example to explain usage of structure:

#include<stdio.h>
#include<string.h>struct Books
{
char title[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”);
Book1.book_id = 1001;

/* book 2 specification */
strcpy( Book2.title, ” Java Programming”);
Book2.book_id = 1002;

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

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

return 0;
}

output:
Book 1 title : C Programming
Book 1 book_id : 1001
Book 2 title : Java Programming
book 2 book_id : 1002

 

Structures as Function Arguments :

You can pass a structure as a function argument in very similar way as you pass any other variable or pointer. 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[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”);
Book1.book_id = 1001;

/* book 2 specification */
strcpy( Book2.title, “Java Programming”);
Book2.book_id = 1002;

/* 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 book_id : %d\n”, book.book_id);
}

output:
Book 1 title : C Programming
Book 1 book_id : 1001
Book 2 title : Java Programming
book 2 book_id : 1002