How to Program C Pointers

How to Program C Pointers thumbnail
Pointers are defined using an asterisk.

The C programming language pointers are variables that contain the address space for another variable. Since pointers "point" to an address space, changing the pointer's value also changes the variable assigned to the pointer. Passed back and forth between functions, pointers allow programmers to control values even when the variable is not global. The C programming language defines pointers using the asterisk prefix.

Instructions

    • 1

      Create your variable. Pointers are assign variable address spaces. Therefore, before you define a pointer, you need a variable. The code below shows you how to define a variable in C:

      int theInt = 0;

    • 2

      Define your pointer. Pointers are easily recognizable in your program, because it has an asterisk prefix. The following code defines a pointer:

      int *ptr;

    • 3

      Assign the variable to the pointer. You assign the address space to the pointer, which is accomplished using the ampersand symbol. Each time you see an ampersand prefix in C, think "address of." The following code assigns the address of the integer to the pointer:

      ptr = &theInt;

      Since "theInt" is defined with a 0 value, the ptr variable contains the value of 0.

    • 4

      Change the value of the variable. Now that the pointer is assigned to the variable, changing the pointer value changes the actual variable as well, The following code shows you how to change variable values using a pointer:

      *ptr = 5;

    • 5

      Print the results to view the value changes. This helps you learn and view the code execution and how it affects pointers. The following code prints the results to your console:

      theInt = 1;
      printf("The value of theInt is %d", theInt); //prints out 5
      *ptr = 10;
      printf("The value of theInt is now %d", theInt); //prints out 10

Related Searches:

References

  • Photo Credit Blue Asterisk Accent image by K. Geijer from Fotolia.com

Comments

You May Also Like

  • Photon Laser Pointers

    Once a high-concept dream of sci-fi movies and scientists, photon laser pointers have become a mainstream part of contemporary life. They have...

  • 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 Program an RCA Universal Remote Control

    It takes just a few steps to program an RCA Universal Remote Control using the Automatic Code Search method. While there may...

  • How to Write a Simple Program in C

    Any text editor such as vi can be used to write a simple program in C. Write and compile a simple program...

  • How to Print the Output Screen of C Program

    The output screen of a C program is the product, usually text or animations, of the code written in the input screen....

  • Why Use a Low Level Programming Language Like Assembly?

    When approaching a development project, programmers make choices about which languages, technologies and platforms to use. This can include both high and...

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

  • Mouse Pointer Options

    Mouse Pointer Options. Part of the series: Computer Hardware & Software Tips. Change the mouse pointer options on a computer by going...

  • How to Convert a Float to a String in C Programming

    Float variable types in programming represent numbers with decimal places included, unlike a standard integer variable type. For floats to be represented...

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

  • How Do I Change My Cursor Without Downloading?

    The standard mouse cursor can be inconspicuous on certain pages. Several designs are available by download, but downloaded elements may come with...

  • How to Declare a Constant String in C Programming

    In C, a character string is a NULL-terminated array of characters. The constant identifier "const" is for variables that do not change...

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

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

  • CCleaner FAQ

    For a computer user, operating a new computer for the first time can be very exciting. Operating it is fast and smooth....

  • How to Customize Your Mouse Pointer

    Mouse pointers can be customized so that the cursor resembles something other than an arrow. Customize a mouse pointer with tips from...

  • How to Use Pointers in Vb

    Visual Basic (VB) provides an easy-to-learn language for web or desktop developers. Pointers are common variables in any language. A pointer "points"...

Related Ads

Featured