C structures

structure

Structure is a keyword, this are reliable to create user defined data type. for example array are store similar type of elements. similar way structure is capable to combine multiple different data in c programming. combination of data is called data member of structures. look at view its syntax

struct tag_name{
	data_members;
	data_members;
}variable_list;
Iteam Description
struct keyword
tag_name name of structure
data_member Use predefined and user defined data
variable_list Variable of structure

for example create a structure of book.

struct Books{
	char  name[30],
	      author[30],
		  serial_number[20];
	int   pages;
	float weight,price;
}book1;

How to use structure in c

there are two steps procedure to utilize structure in c programming. first it should defining the structure. second steps assign data member value. we are use member access operator to assign its value. then utilize its value.

/* 
  Example to use structure in c
*/
/*Header file*/
#include <stdio.h>
#include <string.h>
//defined the structure
struct Books{
	//data members
	char  name[12],
	      author[10],
		  serial_number[7];
	int   pages;
	float weight,price;
};
//display book information
void book_info(struct Books record){
	printf("Name of book    : %s\n",record.name);
	printf("Author Name     : %s\n",record.author);
	printf("Serial Number   : %s\n",record.serial_number);
	printf("Number of Pages : %d\n",record.pages);
	printf("Weight of book  : %fgm\n",record.weight);
	printf("Price of Book   : %f$\n",record.price);
}
int main(){
	//start execution
   
    //create structure Books variables
    struct Books mybook;

    //assign data memeber values
    strcpy(mybook.name,"C Pointer");
    strcpy(mybook.author,"My Self");
    strcpy(mybook.serial_number,"007");
    mybook.pages=100;
    mybook.weight=200.5f;
    mybook.price=1000;
 
    //use variable value
    book_info(mybook);//passing structure to function

	return 0;
}

output

structure Example

in this example we are try to understand main concept of structure.

point Description
struct Books mybook; This is a variable declaration of structure Book. note that is this variable are declare in main function.
book_info(mybook); we are second structure variable mybook to function book_info. in this case as a copy of mybook value to another variable record are store. and printies all data member of structure book_info function.
? garbage value

Output

Name of book    : C Pointer
Author Name     : My Self
Serial Number   : 007
Number of Pages : 100
Weight of book  : 200.500000gm
Price of Book   : 1000.000000$

Structure pointer

Note that when we are passing structure to function. they are copy its value. so best way to use pointer in this case and pass the reference only. for example

	/* 
  Example to use structure in c
*/
/*Header file*/
#include <stdio.h>
#include <string.h>
//defined the structure
struct Books{
	//data members
	char  name[12],
	      author[10],
		  serial_number[7];
	int   pages;
	float weight,price;
};
//display book information
void book_info(struct Books*record){
	printf("Name of book    : %s\n",record->name);
	printf("Author Name     : %s\n",record->author);
	printf("Serial Number   : %s\n",record->serial_number);
	printf("Number of Pages : %d\n",record->pages);
	printf("Weight of book  : %fgm\n",record->weight);
	printf("Price of Book   : %f$\n",record->price);
}
int main(){
	//start execution
   
    //create structure Books variables
    struct Books mybook;

    //assign data memeber values
    strcpy(mybook.name,"C Pointer");
    strcpy(mybook.author,"My Self");
    strcpy(mybook.serial_number,"007");
    mybook.pages=100;
    mybook.weight=200.5f;
    mybook.price=1000;
 
    //use variable value
    book_info(&mybook);//passing structure to function

	return 0;
}
Stack AreamainBooks(struct)mybookname(array) [0] C [1] [2] P [3] o [4] i [5] n [6] t [7] e [8] r [9] \0 [10] ? [11] ? author(array) [0] M [1] y [2] [3] S [4] e [5] l [6] f [7] \0 [8] ? [9] ? serial_number(array) [0] 0 [1] 0 [2] 7 [3] \0 [4] ? [5] ? [6] ? pages(int)= 100weight(float)= 200.5price(float)= 1000book_inforecord(pointer)

Output

Name of book    : C Pointer
Author Name     : My Self
Serial Number   : 007
Number of Pages : 100
Weight of book  : 200.500000gm
Price of Book   : 1000.000000$

important point

1) given tag name are not compulsory of structure. look at view this example.

/* 
  Example of structure in c
*/
/*Header file*/
#include <stdio.h>
#include <string.h>
//defined the structure
struct {
	//data members
	char name[12];
}s1,s2; //variable of structure

int main(){
	//start execution

    //assign data member values
    strcpy(s1.name,"coder");
    strcpy(s2.name,"programmer");
   
     printf("%s\n",s1.name);
     printf("%s\n",s2.name);
	return 0;
}

Output

coder
programmer

Nested structure in c

Syntax

struct outer{
	//data member
	struct inner{
     //data member
	}inner_var;
}outer_var;

Example

/* 
  Example of nested structure in c
*/
/*Header file*/
#include <stdio.h>

//outer structure
struct emplyee{
	int emp_id;
	//data member
	//inner structure
	struct section{
		int section_id;
     //data member
	}section_area;
}e1;
int main(){
	//start execution
  
  //assign data member value
	e1.emp_id=10; // data member
	e1.section_area.section_id=90; //inner structure member

	//print data member value
	printf("emp_id : %d\n",e1.emp_id );
	printf("section_id : %d\n",e1.section_area.section_id );
  return 0;
}

Output

emp_id : 10
section_id : 90

© 2019, regularcodes.com, All rights reserved