eHow launches Android app: Get the best of eHow on the go.

How To

How to Create a Memory Pointer in C

Contributor
By eHow Contributing Writer
(6 Ratings)

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

Difficulty: Moderately Easy
Instructions

    Create Memory Pointer Variables

  1. Step 1

    Understand that memory pointer variables always point to data of a particular type. For instance, a pointer to an int is different from a pointer to a char. However, C will not stop you from freely mixing them up. Do so only if you're sure you know what you're doing.

  2. Step 2

    Create a memory pointer variable by using the syntax you'd use to create a variable of the desired type, but with an asterisk (*) before the variable name, like this:

    int *x;

  3. Step 3

    Consider NULL. Pointers can always be NULL (0), and this typically is used to refer to a pointer that has not yet been set to point anywhere.

  4. Reference and Dereference

  5. Step 1

    Get to know Referencing. Referencing refers to the process of finding the pointer to an existing variable. In C, the referencing operator is the ampersand (&). For instance:

    int color = 5;
    int *pointer_to_color;
    pointer_to_color = &color;

  6. Step 2

    Utilize Dereferencing. Dereferencing is the process of following a pointer to its value, the opposite of referencing. In C the asterisk (*) is used for dereferencing, as follows:

    printf("Color is %d\n", *pointer_to_color); /* prints 5 */

  7. Use Memory Pointer Variables

  8. Step 1

    Pass in a pointer to the variable when you need a function to be able to change a variable, instead of passing the variable's value. This lets the function use dereferencing to change the value:

    void convert_color_to_RGB(int color, int *red, int *green, int *blue) {
    *red = redpart(color);
    *green = greenpart(color);
    *blue = bluepart(color);
    }
    convert_color_to_RGB(15, &myred, &mygreen, &myblue);

  9. Step 2

    Work around C's limits. Whenever you want to pass an array or structure into a function, you must pass a pointer instead, because C only permits single data types to be passed to functions:

    int subtotal(int *scores, int howmany) {
    int total = 0, i;
    for (i = 0; i < howmany; i++) total += scores[i];
    return total;
    }

  10. Step 3

    Create a loop. Since strings are actually arrays of characters, you can create a pointer to a character to loop through a string:

    void replace_character(char *s, char from, char to) {
    char *cp;
    for (cp = s; cp && *cp; cp++) if (*cp == from) *cp = to;
    }

  11. Step 4

    Understand the way C views arrays. Arrays are handled by C as pointers, using pointer arithmetic. C will automatically multiply what you add to a pointer by the size of elements it points to. That means

    scores[5] = 17;
    is exactly the same as
    *(scores + 5) = 17;
    . You can use pointers as a shorthand for array dereferences. For instance,
    *scores = 17;
    always refers to the 0th element of the array.

Tips & Warnings
  • In C, there is a fine line between memory pointers and arrays. Essentially, an array is just a memory pointer pointing to a block of memory that gets automatically allocated.
  • Dereferencing a null pointer is a fatal run-time error. Even if you've coded to ensure a pointer is never null by the time of the dereference, you should code defensively to avoid the error.
  • The actual value of a pointer is unimportant, because the only purpose of a pointer is to dereference it. However, for debugging purposes, you can print the value of a pointer using the formatting code %p. It's a bad idea to attribute much significance to it; what it means depends on the operating system's internal workings.
Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Internet Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

Demand Media
eHow_eHow Technology and Electronics