abrar (5)
abrar (5)
(or)
Important points:
1.The struct keyword is used to define the structure in
the C programming language.
Basic examples:
#include <stdio.h>
#include <string.h>
struct Person {
char name[50];
int citNo;
float salary;
} person1;
int main() {
person1.citNo = 1984;
person1. salary = 2500;
return 0;
}
Output:
Name: George Orwell
Citizenship No.: 1984
Salary: 2500.00
Memory allocation:
1. Arrays in structure:
An array of structres in C can be defined as the
collection of multiple structures variables(objects)
where each variable contains information about
different entities.
The array of structures in C are used to store
information about multiple entities of different
data types.
Declaration:
struct struct_name arr_name [size] = {
{element1_value1, element1_value2, ....},
{element2_value1, element2_value2, ....},
......
......
};
Examples:
#include <stdio.h>
#include <string.h>
struct A {
int var;
};
int main() {
struct A arr[2];
arr[0].var = 10;
arr[1].var = 20;
return 0;
}
Output:
10
20
2.Pointers in structure:
1. Using ( * ) asterisk or indirection operator and dot
( . ) operator.
2. Using arrow ( -> ) operator or membership
operator.
Syntax:
struct struct_name *ptr_name;
Program on pointers in structures:
#include <stdio.h>
struct A {
int var;
};
int main() {
struct A a = {30};
struct A *ptr;
ptr = &a;
printf("%d", ptr->var);
return 0;
}
Output:
30