Structure in C
Structure in C
USES OF STRUCTURE
Structure in C programming is very helpful in cases where we
need to store similar data of multiple entities. understand the
need for structures with a real-life example. Suppose you need to
manage the record of books in a library. Now a book can have
properties like book_name, author_name, and genre
C Structure Declaration
We have to declare structure in C before using it in our program. In
structure declaration, we specify its member variables along with
their datatype. We can use the struct keyword to declare the
structure in C using the following syntax:
Syntax
struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
};
The above syntax is also called a structure template or structure
prototype and no memory is allocated to the structure in the
declaration.
C Structure Definition
To use structure in our program, we have to define its instance. We can do
that by creating variables of the structure type. We can define structure
variables using two methods:
struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
}variable1, varaible2, ...;
void main()
{
// Create a structure variable of myStructure called s1
struct myStructure s1;
// Print values
printf("My number: %d\n", s1.myNum);
printf("My letter: %c\n", s1.myLetter);
String in structure
Example
struct myStructure
{
char myString[30]; // String
};
void main()
{
struct myStructure s1;
struct Books
{
char title[50];
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 = 649;
/* book 2 specification */
strcpy( Book2.title, "maths");
Book2.book_id = 640;
/* print Book1 info */
printf( "Book 1 title : %s\n", Book1.title);
printf( "Book 1 book_id : %d\n", Book1.book_id);
#include<stdio.h>
struct Car {
char brand[50];
char model[50];
int year;
};
int main()
{
struct Car car1 = {"BMW", "X5", 1999};
struct Car car2 = {"Ford", "Mustang", 1969};
struct Car car3 = {"Toyota", "Corolla", 2011};
printf("%s %s %d\n",car1.brand,car1.model,
car1.year);
printf("%s %s %d\n",car2.brand,car2.model,
car2.year);
printf("%s %s %d\n",car3.brand,
car3.model, car3.year);
}
Program to make structure of name,id and marks of 3 students.
#include<stdio.h>
struct student
{
char name[20];
int id;
float marks;
};
int main()
{
struct student s1,s2,s3;
int main()
{
// creating structure variables using new names
str2 s1 = { 314 };
printf(" %d", s1.x);
}
void main ()
{
emp e = {10, "ramu”, 5000};
printf("number = %d\n”, e.eno);
printf("name = %s\n”, e.ename);
printf("salary = %f\n”, e.sal);
getch ();
}
Structure Pointer in C
Structure Pointer
We can define a pointer that points to the structure like any other
variable. Such pointers are generally called Structure Pointers. We
can access the members of the structure pointed by the structure
pointer using the ( -> ) arrow operator.
Initialization of the Structure Pointer
1. ptr = &structure_variable;
struct point
{
int value;
};
int main()
{
struct point s;
There are two ways to access the members of the structure with the
help of a structure pointer:
1. With the help of (*) asterisk or indirection operator and (.) dot
operator.
2. With the help of ( -> ) Arrow operator.
Program to access the structure member using structure pointer and
the dot operator
#include <stdio.h>
struct Subject
{
char sub_name[30];
int sub_id;
};
void main()
{
struct Subject sub; // declare the Subject variable
struct Subject *ptr; // create a pointer variable (*ptr)
ptr = ⊂ /* ptr variable pointing to the address of the structur
e variable sub */
strcpy (sub.sub_name, " Computer Science");
sub.sub_id = 1201;
struct Point
{
int x, y;
};
int main()
{
struct Point str = { 1, 2 };
#include <stdio.h>
Struct person
{
int age;
float weight;
};
int main( )
{
Struct person *personPtr, person1;
personPtr = &person1;
printf("Displaying:\n");
printf("Age: %d\n", personPtr->age);
printf("weight: %f", personPtr->weight);
return 0;
}
Structures as Function Arguments
You can pass a structure as a function argument in the same way as
you pass any other variable or pointer.
#include <stdio.h>
#include <string.h>
struct Books
{
char title[50];
int book_id;
};
/* function declaration */
void printBook( struct Books B1);
void main( )
{
printBook( B1 );
int main()
{
struct student s1;
In the above code, we created a structure to hold the name, roll number, and
percentage of the student. The input from the user is stored in the structure.
A function named display() is created, which takes the roll number and the
percentage of the student as the parameter. Using the dot (.) operator, we
accessed the member of the structure and passed it to the function.
Displaying information
Roll number: 42
Percentage: 98