Derived data types are complex data types that are built using the basic (primary) data types. They are used to store and manage collections of data or to handle more advanced programming concepts like memory management. The primary derived types in C are Arrays, Pointers, and Functions.
1. Arrays ⛓️
An array is a fixed-size, sequential collection of elements of the same data type. Think of it as a row of numbered containers, where each container holds the same kind of item.
· Purpose: To store multiple related values under a single variable name.
·
Declaration: data_type array_name[size];
Example
This example stores the marks of 5 subjects in an integer array. Array elements are accessed using an index, which starts from 0.
C
#include <stdio.h>
int main() {
// Declare and initialize an array of 5 integers
int marks[
5] = {
85,
90,
78,
92,
88};
printf(
"Displaying marks of subjects:\n");
// Use a loop to access each element of the array
// The index goes from 0 to 4
for (
int i =
0; i <
5; i++) {
// marks[i] accesses the element at the current index
printf(
"Subject %d: %d\n", i +
1, marks[i]);
}
return
0;
}
2. Pointers 👉
A pointer is a special variable that does not store data directly but instead stores the memory address of another variable. It "points to" the location where the data is stored.
· Purpose: Pointers are essential for dynamic memory allocation, creating complex data structures (like linked lists), and for allowing functions to modify the original variables passed to them.
·
Declaration: data_type *pointer_name;
o The asterisk (*
) indicates that it's a pointer.
Example
This example shows how a pointer stores the address of a variable and how to access the variable's value through the pointer.
C
#include <stdio.h>
int main() {
int age =
25;
// A normal integer variable
// A pointer 'ptr' that can store the address of an integer
int *ptr;
// The '&' operator gets the memory address of 'age'
ptr = &age;
printf(
"Value of age (direct access): %d\n", age);
// The '*' operator dereferences the pointer to get the value it points to
printf(
"Value of age (via pointer): %d\n", *ptr);
printf(
"Memory address of age: %p\n", (
void *)ptr);
return
0;
}
3. Functions ⚙️
A function is a self-contained block of code that performs a specific task. While not a data type in the same way as an array, it's considered derived because its definition involves other data types for its parameters and return value.
· Purpose: To break down a large program into smaller, reusable, and manageable modules, improving readability and maintainability.
·
Declaration
(Prototype): return_type
function_name(parameter_type1, parameter_type2);
Example
This example
defines a simple function add
that takes two integers and returns their sum. The main
function calls this add
function.
C
#include <stdio.h>
// Function prototype (declaration)
int add(int a, int b);
// The main function - program execution starts here
int main() {
int num1 =
10;
int num2 =
20;
int sum;
// Calling the 'add' function and storing its return value
sum = add(num1, num2);
printf(
"The sum of %d and %d is: %d\n", num1, num2, sum);
return
0;
}
// Function definition
int add(int a, int b) {
// The 'return' keyword sends a value back to the caller
return a + b;
}