Structure
What is the structure in C?
Syntax to define a structure
struct structure_name
{
member1_declaration;
member2_declaration;
...
...
memberN_declaration;
};
Example to define a structure
struct student
{
char name[40]; // Student name
int age; // Student age
unsigned long mobile; // Student mobile number
};
Points to remember while structuring definition
- You must terminate the structure definition with a semicolon;.
- You cannot assign the value to members inside the structure definition, it will cause a compilation error. Since you are defining type you aren't associating data.
struct student { char name[40] = "Raj"; int age = 26; unsigned long mobile = 9891000033; };
- You can define a structure anywhere like global scope (accessible by all functions) or local scope(accessible by particular function).
- Structure member definition may contain other structure type.
How to create a structure object (structure variable)?
Declaration along with the structure definition
struct structure_name
{
member1_declaration;
member2_declaration;
...
...
memberN_declaration;
}structure_variable;
struct student
{
char name[40]; // Student name
int age; // Student age
unsigned long mobile; // Student mobile number
}student1;
Declaration after structure definition
struct structure_name structure_variable;
struct student
{
char name[40]; // Student name
int age; // Student age
unsigned long mobile; // Student mobile number
};
// Declare student variable
struct student student1;
How to access structure members (data)?
- Dot/period operator.
- Arrow operator ->
Dot/period operator (.) in C
structure_variable.member_name;
// Assign age of student1
student1.age = 26;
Arrow operator (->) in C
pointer_to_structure->member_name;
// Student2 is a pointer to student type
student2->age = 29;
Example program to demonstrate declare, define and access structure members
/**
* C program to demonstrate structures.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Student type definition
struct student
{
char name[40]; // Student name
int age; // Student age
unsigned long long mobile; // Student mobile number
};
int main()
{
struct student student1; // Simple structure variable
struct student *student2; // Pointer to student
// Initialize pointer to student through dynamic memory allocation
student2 = (struct student*) malloc(sizeof(struct student));
// Input data in structure members using dot operator
printf("Enter student name: ");
fgets(student1.name, 40, stdin);
printf("Enter student age: ");
scanf("%d", &student1.age);
printf("Enter student mobile: ");
scanf("%llu", &student1.mobile);
// Eat new line from input buffer
getchar();
printf("\nStudent using simple structure variable.\n");
printf("Student name: %s\n", student1.name);
printf("Student age: %d\n", student1.age);
printf("Student mobile: %llu\n\n", student1.mobile);
// Input data in pointer to structure type using arrow operator
printf("Enter student name: ");
fgets(student2->name, 40, stdin);
printf("Enter student age: ");
scanf("%d", &student2->age);
printf("Enter student mobile: ");
scanf("%llu", &student2->mobile);
printf("Student using pointer to structure variable.\n");
printf("Student name: %s\n", student2->name);
printf("Student age: %d\n", student2->age);
printf("Student mobile: %llu\n", student2->mobile);
// Clear memory that was alloacted dynamically
free(student2);
return 0;
}
Output
No comments