C Data Structures: Arrays, Structs, Unions, and More
Arrays
Arrays are a data structure that stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data.
Structures
To define a structure, use the struct statement. This statement defines a new data type with multiple members. Structures can contain array members and can also be treated as arrays.
Unions
A union is a special data type that allows storing different data types in the same memory location. A union can have multiple members, but only one member can hold a value at any given time. Unions provide an efficient way to use the same memory location for multiple purposes.
Enumerations
Enumeration (or enum) is a user-defined data type in C. It is mainly used to assign names to integral constants, making a program easier to read and maintain.
Pointers
A pointer is a variable whose value is the address of another variable. Like any variable, you must declare a pointer before using it to store a variable address. Pointers can be declared using *. A null pointer has a special value and does not point to any address.
String Functions
The string.h header defines a variable type, a macro, and various functions for manipulating arrays of characters.
- The function
int strcmp(const char *str1, const char *str2)compares the string pointed to bystr1to the string pointed to bystr2. - The function
char *strncat(char *dest, const char *src, size_t n)appends the string pointed to bysrcto the end of the string pointed to bydest, up toncharacters long. strncpy()prevents buffer overflow, whilestrcpy()can cause buffer overflow.- The function
char *strcpy(char *dest, const char *src)copies the string pointed to bysrctodest.
Pointer Arithmetic
You can perform various arithmetic operations on pointers, such as increment, decrement, and comparison.
Typedef
The C programming language provides the keyword typedef, which you can use to give a type a new name. For example, to define BYTE for one-byte numbers:
typedef unsigned char BYTE;
BYTE b1, b2;Memory Allocation
The function malloc() reserves a block of memory of a specified size and returns a pointer of type void, which can be cast into a pointer of any form.
ptr = (cast-type*) malloc(byte-size);
ptr = (int*) malloc(100 * sizeof(int));The only difference between malloc() and calloc() is that malloc() allocates a single block of memory, whereas calloc() allocates multiple blocks of memory, each of the same size, and sets all bytes to zero.
ptr = (float*) calloc(25, sizeof(float));Dynamically allocated memory created with either calloc() or malloc() does not get freed on its own. You must explicitly use free() to release the space.
free(ptr);Linked Lists
A linked list is an extensible data structure consisting of nodes. Each node contains application-specific data and a pointer to another node. The program only needs to remember the pointer to the first node.
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
void printList(){
struct node *ptr = head;
printf("\n[head] =>");
while(ptr != NULL){
printf(" %d =>",ptr->data);
ptr = ptr->next;
}
printf(" [null]\n");
}C Functions
A function is a group of statements that together perform a task. Every C program has at least one function, which is main(). Most programs define additional functions.
A function declaration tells the compiler about a function’s name, return type, and parameters. A function definition provides the actual body of the function.
Iterators
An iterator is a mechanism to advance through some data structure without revealing its underlying implementation.
