WINSEM2021-22 BCSE102L TH VL2021220504672 2022-03-24 Reference-Material-I
WINSEM2021-22 BCSE102L TH VL2021220504672 2022-03-24 Reference-Material-I
Define Structures
Before you can create structure variables, you need to define its data type.
To define a struct, the struct keyword is used.
Syntax of struct
struct structureName {
dataType member1;
dataType member2;
...
};
For example,
struct Person {
char name[50];
int citNo;
float salary;
};
Here, a derived type struct Person is defined. Now, you can create
variables of this type.
Create struct Variables
When a struct type is declared, no storage or memory is allocated. To
allocate memory of a given structure type and work with it, we need to
create variables.
Here's how we create structure variables:
struct Person {
// code
};
int main() {
struct Person person1, person2, p[20];
return 0;
}
struct Person {
// code
} person1, person2, p[20];
In both cases,
1. . - Member operator
2. -> - Structure pointer operator
Suppose, you want to access the salary of person2 . Here's how you can do
it.
person2.salary
Example 1:
#include <stdio.h>
#include <string.h>
int main() {
return 0;
}
Output
This is because name is a char array (C-string) and we cannot use the
assignment operator = with it after we have declared the string.
Finally, we printed the data of person1 .
Keyword typedef
We use the typedef keyword to create an alias name for data types. It is
commonly used with structures to simplify the syntax of declaring variables.
For example, let us look at the following code:
struct Distance{
int feet;
float inch;
};
int main() {
struct Distance d1, d2;
}
int main() {
distances d1, d2;
}
Nested Structures
You can create structures within a structure in C programming. For
example,
struct complex {
int imag;
float real;
};
struct number {
struct complex comp;
int integers;
} num1, num2;
Suppose, you want to set imag of num2 variable to 11. Here's how you can
do it:
num2.comp.imag = 11;
Example 3: Nested Structures
#include <stdio.h>
struct complex {
int imag;
float real;
};
struct number {
struct complex comp;
int integer;
} num1;
int main() {
return 0;
}
Output
Imaginary Part: 11
Real Part: 5.25
Integer: 6
Why structs in C?
Suppose, you want to store information about a person: his/her name,
citizenship number, and salary. You can create different
variables name , citNo and salary to store this information.
What if you need to store information of more than one person? Now, you
need to create different variables for each information per
person: name1 , citNo1 , salary1 , name2 , citNo2 , salary2 , etc.
A better approach would be to have a collection of all related information
under a single name Person structure and use it for every person.
struct name {
member1;
member2;
.
.
};
int main()
{
struct name *ptr, Harry;
}
#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;
}
Now, you can access the members of person1 using the personPtr pointer.
By the way,
#include <stdio.h>
#include <stdlib.h>
struct person {
int age;
float weight;
char name[30];
};
int main()
{
struct person *ptr;
int i, n;
printf("Displaying Information:\n");
for(i = 0; i < n; ++i)
printf("Name: %s\tAge: %d\n", (ptr+i)->name, (ptr+i)->age);
return 0;
}
#include <stdio.h>
struct student {
char name[50];
int age;
};
// function prototype
void display(struct student s);
int main() {
struct student s1;
return 0;
}
Output
#include <stdio.h>
struct student
{
char name[50];
int age;
};
// function prototype
struct student getInformation();
int main()
{
struct student s;
s = getInformation();
printf("\nDisplaying information\n");
printf("Name: %s", s.name);
printf("\nRoll: %d", s.age);
return 0;
}
struct student getInformation()
{
struct student s1;
return s1;
}
#include <stdio.h>
typedef struct Complex
{
float real;
float imag;
} complex;
int main()
{
complex c1, c2, result;
printf("For first number,\n");
printf("Enter real part: ");
scanf("%f", &c1.real);
printf("Enter imaginary part: ");
scanf("%f", &c1.imag);
return 0;
}
void addNumbers(complex c1, complex c2, complex *result)
{
result->real = c1.real + c2.real;
result->imag = c1.imag + c2.imag;
}
Output
result.real = 4.5
result.imag = -5.6
union car
{
char name[50];
int price;
};
union car
{
char name[50];
int price;
};
int main()
{
union car car1, car2, *car3;
return 0;
}
union car
{
char name[50];
int price;
} car1, car2, *car3;
In both cases, union variables car1 , car2 , and a union pointer car3 of union
#include <stdio.h>
union unionJob
{
//defining a union
char name[32];
float salary;
int workerNo;
} uJob;
struct structJob
{
char name[32];
float salary;
int workerNo;
} sJob;
int main()
{
printf("size of union = %d bytes", sizeof(uJob));
printf("\nsize of structure = %d bytes", sizeof(sJob));
return 0;
}
Output
size of union = 32
size of structure = 40
#include <stdio.h>
union Job {
float salary;
int workerNo;
} j;
int main() {
j.salary = 12.3;
Output
Salary = 0.0
Number of workers = 100
C Struct Examples
Store information of a student using structure