Error Handling in C
Ensure robust C programs — learn error detection with return codes, errno, perror/strerror, assertions, memory and file I/O error handling with clear examples.

Unlock C’s power with pointers—discover how to declare, reference, dereference, use NULL & void pointers, and master pointer arithmetic with clear examples.
A pointer is a variable that stores the memory address of another variable. It enables indirect access to the value stored at that memory address. Pointers are particularly useful for tasks like memory manipulation, passing values by reference, and dynamic memory allocation.
Table of contents [Show]
To declare a pointer, use the * symbol followed by the data type it points to. For example:
int *ptr; // Declares a pointer to an integer
Pointers should be initialized with a valid memory address before they are used. They can be initialized in several ways:
int x = 10;
int *ptr = &x; // Pointer initialized with the address of x
To access the value pointed to by a pointer, use the dereference operator *:
int value = *ptr; // Retrieves the value pointed to by ptr
Pointers can be incremented and decremented like regular variables:
ptr++; // Moves the pointer to the next memory location
Pointers can be used in arithmetic operations:
int arr[5];
int *ptr = arr;
ptr += 2; // Moves the pointer two elements ahead
Arrays and pointers are closely related in C. An array name can be treated as a pointer to its first element:
int arr[5];
int *ptr = arr; // arr and ptr now point to the same memory location
Pointers are often used to pass values by reference to functions, allowing the function to modify the original data:
void modifyValue(int *ptr) {
*ptr = 42;
}
int main() {
int x = 10;
modifyValue(&x); // Pass the address of x to the function
}
Functions can return pointers to allocated memory. Just ensure the allocated memory persists after the function returns:
int *createArray(int size) {
int *arr = (int *)malloc(size * sizeof(int));
return arr;
}
Pointers can also point to functions, enabling dynamic function invocation:
int add(int a, int b) {
return a + b;
}
int (*funcPtr)(int, int) = add; // Pointer to the add function
int result = funcPtr(3, 5); // Calls add(3, 5) through the pointer
malloc(size_t size): Allocates memory of the given size and returns a pointer to the first byte.calloc(size_t num, size_t size): Allocates memory for an array of elements and initializes them to zero.realloc(void *ptr, size_t size): Resizes a previously allocated memory block.free FunctionAlways free memory allocated using malloc, calloc, or realloc to prevent memory leaks:
int *arr = (int *)malloc(5 * sizeof(int));
free(arr);
Failing to free allocated memory leads to memory leaks. Dangling pointers are pointers that still reference memory after it's been freed.
Arrays can be accessed using pointers and pointer arithmetic:
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
int thirdElement = *(ptr + 2); // Accesses the third element of the array
Pointers can point to entire arrays, enabling array manipulation:
int arr[5];
int (*arrPtr)[5] = &arr; // Pointer to an array of 5 integers
Pointers can be used to access members of a structure:
struct Person {
char name[50];
int age;
};
struct Person person;
struct Person *ptr = &person;
ptr->age = 30; // Accesses age using pointer
The arrow operator (->) is a convenient way to access structure members through pointers:
ptr->age = 30; // Equivalent to (*ptr).age = 30;
NULL when they don't point to valid memory.Pointers are powerful but require careful handling. Practice and understanding their concepts thoroughly will lead to effective and efficient C programming.
Ensure robust C programs — learn error detection with return codes, errno, perror/strerror, assertions, memory and file I/O error handling with clear examples.
Unlock flexibility in C—learn how to implement variadic functions using stdarg.h: handling ellipses (...), va_list, va_start, va_arg, va_end, with clear examples.
Unlock the power of C preprocessors — learn directives like #include, #define, macros, conditional compilation, and file inclusion for cleaner, flexible code.
These cookies are essential for the website to function properly.
These cookies help us understand how visitors interact with the website.
These cookies are used to deliver personalized advertisements.


