Code About Us Tutorial


  Pointers and References
    What are Pointers?
    Declaring Pointers
    The Indirection and Dereference operator
    What is a Reference?
    Using References
    Using Pointers and References
    The new Keyword
    The delete Keyword
    Passing By Reference
    Passing by Value


   
 

Home > Pointers and References > Declaring Pointers

November 29, 2009 8:25 pm

   

Declaring Pointers

Now that we understand that every variable has an address, we will go on and learn about how to store this address into another variable (a pointer).

The syntax of a pointer is as follows:

Type *pointer-name = &variable-name;

This syntax is for assigning a pointer the memory address of another variable, here is what it looks like.

 

 
#include <iostream.h>

int main() { int n = 10; //variable int *p = &n;

cout <<"Here is the address of n: " << &n << endl; cout <<"Here is the value of n: " << n << "n"; cout <<"Here is the value of *p: " << *p << "n"; return 0; }

As you can see, the value is the same, because the pointer was holding the value of n. Please Rate this Code:


    Comments for: Declaring Pointers
Annonymouse User says:
There is error ending of cout line.To change to new line character should be \"/n\" not the \"n\".

MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
Very Sorry, there has been some parse errors. Our parser went crazy at one point, and then after I fixed it, I realized that it had done permanent damage to many pages. The only way to fix this is on a page to page basis... which is not going to happen anytime soon.

-MoMad

A.D [ad@hotmail.com] Posted: 7 times. says:
it is quiet easy !!well done !!

mike [decksunlimited@avaloninternet.net] Posted: 2 times. says:
Don\'t get the logic of the \"n\" on two of the lines of code?

mike [decksunlimited@avaloninternet.net] Posted: 2 times. says:
Don\'t get the logic of the \"n\" on two of the lines of code? says:
newline is "\n" not "/n"..




Add Comment:
Name:
Email:


 «1» THINKQUEST TEAM C0111571 © 2001. All Rights Reserved.