Derived data types in C language are types that are created from the basic data types. They allow programmers to create more complex data structures that can store multiple values or represent more complex relationships between data. The primary derived data types in C include:
- Arrays
- Pointers
- Structures
- Unions
- Function Types
Let’s go through each of these derived data types in detail.
1. Arrays
An array is a collection of elements of the same type, stored in contiguous memory locations. Arrays allow you to work with multiple values of the same type using a single identifier.
Declaration: An array is declared by specifying the type of its elements and the number of elements.
- Syntax:
data_type array_name[array_size];
- Syntax:
Example:
int numbers[5]; // Declares an array of 5 integers float prices[10]; // Declares an array of 10 floats
Accessing Elements: Array elements are accessed using their index (starting from 0).
numbers[0] = 10; // Assigns 10 to the first element of the array printf("%d\n", numbers[0]); // Prints 10
2. Pointers
A pointer is a variable that stores the memory address of another variable. Pointers are powerful and allow for dynamic memory allocation, efficient array manipulation, and the creation of complex data structures like linked lists.
- Declaration: A pointer is declared by specifying the type it points to, followed by an asterisk (*).
- Syntax:
data_type *pointer_name;
- Syntax:
- Example:
int num = 10; int *ptr = # // ptr now holds the address of num printf("%d\n", *ptr); // Prints 10 (dereferencing the pointer)
3. Structures
A structure is a user-defined data type that allows you to group different types of variables under a single name. Structures are useful for representing complex data entities.
- Declaration: Structures are declared using the
struct
keyword.- Syntax:
struct structure_name { data_type member1; data_type member2; // ... };
- Syntax:
- Example:
struct Student { char name[50]; int age; float grade; }; struct Student student1; // Declare a structure variable student1.age = 20; // Assign values to structure members
4. Unions
A union is similar to a structure but allows you to store different data types in the same memory location. A union can hold only one of its non-static data members at a time, which saves memory.
- Declaration: Unions are declared using the
union
keyword.- Syntax:
union union_name { data_type member1; data_type member2; // ... };
- Syntax:
- Example:
union Data { int i; float f; char str[20]; }; union Data data1; // Declare a union variable data1.i = 10; // Assign an integer value printf("%d\n", data1.i); // Prints 10 data1.f = 220.5; // Now assign a float value printf("%d\n", data1.i); // Prints an unpredictable value
5. Function Types
In C, functions can also be treated as data types. A function can return a value of a specific type, and pointers can be used to point to functions, allowing for dynamic function calls.
Function Declaration:
- Syntax:
return_type function_name(parameters) { // function body }
- Syntax:
Function Pointer: A pointer that points to a function.
- Example:
void greet() { printf("Hello!\n"); } void (*funcPtr)() = greet; // Pointer to a function funcPtr(); // Calls the function
- Example:
Example Program Using Derived Data Types
Here's a simple program that demonstrates the use of derived data types:
#include <stdio.h>
// Structure definition
struct Student {
char name[50];
int age;
float grade;
};
int main() {
// Using an array
int scores[5] = {85, 90, 75, 80, 95};
// Using pointers
int *ptr = scores;
// Using structure
struct Student student1;
student1.age = 20;
student1.grade = 89.5;
sprintf(student1.name, "Alice");
// Print scores using pointer
printf("Scores: ");
for (int i = 0; i < 5; i++) {
printf("%d ", *(ptr + i)); // Dereferencing pointer
}
printf("\n");
// Print student information
printf("Name: %s, Age: %d, Grade: %.2f\n", student1.name, student1.age, student1.grade);
return 0;
}
Output
Scores: 85 90 75 80 95
Name: Alice, Age: 20, Grade: 89.50
Summary
- Derived Data Types: Include arrays, pointers, structures, unions, and function types, enabling more complex data handling.
- Usage: These types allow for efficient data organization and manipulation, making C a powerful programming language for systems and applications development.
Understanding derived data types is essential for effectively managing data and creating robust programs in C.