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.

  1. 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.

Related Searches:

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.

Comments

You May Also Like

  • How to Create a Memory Pointer in C

    C makes extensive use of memory pointers for handling arrays, function calls, data structures and much more. Knowing how to work directly...

  • Mouse Pointer Options

    Change the mouse pointer options on a computer by going to the "Control Panel," clicking on "Mouse" and adjusting the speed for...

  • How to Use the "This" Pointer in C++

    In a C++ program, if you create object A of class X, you can then obtain the address of A by using...

  • How to Understand Pointers in C

    The C programming language provides you with a data type called a pointer. A pointer "points" to another variable in your software...

  • The Disadvantages of Pointers in C

    The Disadvantages of Pointers in C. In the C programming language, pointers store address information for memory locations in the computer. They...

  • How to Use C Arrays in C++

    A C array is a data structure that stores many elements of the same data type. C arrays, when you use them...

  • Mouse Pointer Types

    Mouse Pointer Types. You can turn almost any shape, picture or graphic design into a mouse pointer, also called a cursor. The...

  • How to Use Pointers in C++

    Often, programmers prefer to deal with chunks of data through their memory location instead of directly. C++ pointers are ideal tools for...

  • How to Use Void Pointers in C

    Pointers are one of the most powerful -- and most difficult -- features of the C programming language. When working with pointers,...

  • How to Print Pointers in C

    Pointers provide programmers with the ability to alias a variable by "pointing" to its address space. A pointer is assigned a variable's...

  • What Is the Difference Between the Dot Notation & Arrow Notation in C?

    Programmers use the C programming language for systems software and low-level code. Because of this, one of the capabilities of C is...

  • How to Debug a Memory Leak in C++

    A memory leak is when a program causes a incremental but steady consumption of memory, causing the program to use more memory...

  • How to Create an Array in C

    Arrays offer the most efficient method for storing lists of data in C. They're very easy for the programmer to create and...

Related Ads

Featured