Derived Data Types in C with Example Programs
Table of Contents
- Introduction to Derived Data Types
- Arrays
- Pointers
- Structures
- Unions
- Example Programs
- Example 1: One-dimensional Array
- Example 2: Multi-dimensional Array
- Example 3: Pointers and Arrays
- Example 4: Pointers to Functions
- Example 5: Defining and Declaring Structures
- Example 6: Nested Structures
- Example 7: Structures and Functions
- Example 8: Introduction to Unions
- Example 9: Unions vs. Structures
Introduction to Derived Data Types
Derived data types in C are data types that are built using the basic data types. These data types allow programmers to create more complex and structured data structures. The three main derived data types in C are arrays, pointers, structures, and unions.
Arrays
One-dimensional Arrays
A one-dimensional array is a linear collection of elements of the same data type.
#include <stdio.h>
int main() {
int marks[5] = {85, 92, 78, 88, 90};
// Accessing array elements
printf("First element: %d\n", marks[0]);
printf("Second element: %d\n", marks[1]);
// ... and so on
return 0;
}
Multi-dimensional Arrays
A multi-dimensional array is an array of arrays. It can be thought of as a table with rows and columns.
#include <stdio.h>
int main() {
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Accessing array elements
printf("Element at matrix[1][2]: %d\n", matrix[1][2]);
// ... and so on
return 0;
}
Pointers
Introduction to Pointers
A pointer is declared using the * symbol.
#include <stdio.h>
int main() {
int number = 42;
int *ptr; // Pointer declaration
ptr = &number; // Pointer initialization with the address of 'number'
printf("Value of 'number': %d\n", number);
printf("Address of 'number': %p\n", &number);
printf("Value of '*ptr': %d\n", *ptr); // Dereferencing the pointer
return 0;
}
Pointers and Arrays
Pointers and arrays are closely related. In fact, arrays are accessed using pointers.
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr; // Pointer initialized with the base address of 'arr'
printf("First element of 'arr': %d\n", arr[0]);
printf("First element of 'arr' using '*ptr': %d\n", *ptr);
return 0;
}
Pointers to Functions
Pointers to functions are used to store the address of a function, allowing us to call the function indirectly.
#include <stdio.h>
void greet() {
printf("Hello, World!\n");
}
int main() {
void (*func_ptr)(); // Pointer to a function taking no arguments and returning void
func_ptr = &greet; // Assigning the address of 'greet' function to 'func_ptr'
(*func_ptr)(); // Calling the function using the function pointer
return 0;
}
Structures
Defining and Declaring Structures
A structure is defined using the struct keyword.
#include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
struct Point p1; // Structure variable declaration
p1.x = 10; // Accessing structure members
p1.y = 20;
printf("Coordinates: (%d, %d)\n", p1.x, p1.y);
return 0;
}
Nested Structures
You can define structures within structures to create nested structures.
#include <stdio.h>
struct Date {
int day;
int month;
int year;
};
struct Student {
char name[50];
int roll_number;
struct Date dob; // Nested structure
};
int main() {
struct Student stu1 = {"John Doe", 101, {5, 8, 2000}};
printf("Name: %s\n", stu1.name);
printf("Roll Number: %d\n", stu1.roll_number);
printf("Date of Birth: %d/%d/%d\n", stu1.dob.day, stu1.dob.month, stu1.dob.year);
return 0;
}
Structures and Functions
Structures can be passed as arguments to functions.
#include <stdio.h>
struct Rectangle {
int length;
int width;
};
int calculateArea(struct Rectangle r) {
return r.length * r.width;
}
int main() {
struct Rectangle rect = {5, 10};
int area = calculateArea(rect);
printf("Area of the rectangle: %d\n", area);
return 0;
}
Unions
Introduction to Unions
A union is defined using the union keyword.
#include <stdio.h>
union Data {
int x;
float y;
char z;
};
int main() {
union Data d;
d.x = 65;
printf("Value of 'x': %d\n", d.x);
d.y = 3.14;
printf("Value of 'y': %.2f\n", d.y);
d.z = 'A';
printf("Value of 'z': %c\n", d.z);
return 0;
}
Unions vs. Structures
The main difference between unions and structures is that a union can store only one member at a time, whereas a structure can store multiple members simultaneously.
Example Programs
The examples provided above showcase the usage of derived data types in C. By understanding these concepts and practicing with more examples, you can effectively utilize derived data types to build complex data structures and manipulate data efficiently in C programming.
Feel free to explore further and create your own programs to gain a deeper understanding of derived data types in C! Happy coding!
0 Comments