What Is a Pointer in C ?
One of the features that makes C such a powerful programming language is the ability for programmers to access memory directly using pointers. Pointers are used extensively in C for handling arrays of data, manipulating character strings, passing parameters to functions and creating linked lists and other complex data structures. Pointers are one of the greatest sources of confusion for programmers new to the C language. Understanding and using pointers is critical to effective C programming.
-
Function
-
An object pointer, or simply pointer, is a variable that contains a memory address where an object, such as a struct or float, or an array of objects, is stored. With that memory address, the programmer can read, modify and move through the memory using a pointer.
-
Benefits
-
Functions that take a pointer to an external object passed in as a parameter can modify that object. When the function exits, the new value assigned to that object will persist. A function can take multiple pointers, allowing a single function to modify multiple objects with a single function call.
A pointer can be used to navigate through an array of elements systematically or randomly using array notation, iteration or basic math. Using pointers for array access is faster than the more complicated implementations of similar functionality found in other languages. While such implementations are often easier to use and prevent errors, there is additional overhead that affects performance.
Significance
-
The ability to dynamically allocate arbitrary amounts of heap memory during runtime is a technique called "dynamic memory allocation." Many earlier languages, such as Fortran, required the amount of memory allocated for structures such as arrays to be set at compile time, and the amount of memory allocated could not be changed during the running of the program. Pointers are used in C to hold the address of dynamically allocated memory.
Usage
-
A pointer is declared by placing a star (*) between the type name and the variable name.
The value of an object in memory at the address held by the pointer is accessed by "dereferencing" the pointer. Dereferencing a pointer is done by putting a star (*) before the pointer name.
When handling pointers to structs, a member of the struct is referenced by using an arrow (->) between the pointer name and the member name.
The address of an object can be accessed by placing an ampersand (&) before the object's variable name. Passing an object's address to a function is called "pass by reference." The parameter is declared as a pointer in the function prototype. The function dereferences the parameter to modify its value, and the value will persist when the function exits.
Arrays are created by allocating the amount of memory required to hold the desired number of objects of a given type. This is accomplished during or after declaration with pointer notation and malloc or calloc, or during declaration using array notation. Elements of an array can be accessed using index reference notation or by iterating the array pointer. Iteration automatically increments or decrements the address by the size of an individual element so the pointer is positioned correctly at the beginning of the next or previous element.
Warning
-
Memory allocation and deallocation is managed by the programmer. Memory that has been allocated must be deallocated when it is no longer needed. Failing to deallocate memory prevents it from being used until the application exits. A "memory leak" occurs when a reference to allocated memory is lost without freeing the memory first. Memory leaks can happen when a pointer is reassigned, or a function exits without delegating ownership of the allocated memory.
Accessing memory beyond the end of allocated memory is a common error when working with arrays. This error is called having an "array index out of bounds." This may cause the application to behave incorrectly or exit unexpectedly.
References
Resources
- Photo Credit "snow patrol:make this go on forever" is Copyrighted by Flickr user: visualpanic (Lali Masriera) under the Creative Commons Attribution license.