1. Home
  2. Computing & Technology
  3. C / C++ / C#

C++ Tutorial - About Pointers

By David Bolton, About.com

5 of 10

Examples of Pointers

// ex1.cpp

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
  char * Fred="text";
  char * Okfred;
  int a=46;
  int * d=&a;
  char * Fred2=Fred;
  int * ptra = &a;
  int c = *ptra;
  int b[2];
  int * Ptrb=b;
  *++ptra;
  ++(*d) ;
  int * myint= new int;

  Okfred=(char *)new char[strlen("david")+1];
  Fred=Okfred;
  *Okfred++='d';
  *Okfred++='a';
  *Okfred++='v';
  *Okfred++='i';
  *Okfred++='d';
  *Okfred= '\0';

  *(Fred+1)='2';

  cout << "c = " << c<< endl;
  cout << "*d= "<< *d << endl;
  cout << "d= " << d<< endl;
    cout << "Fred = "<< Fred << endl;
  cout << "Fred2 = "<< Fred2<< endl;
  cout << "Address of b is "<< b<< endl;
  cout << "Address of b[0] is " << &b[0]<< endl;
  cout << "Address of b[1] is " << &b[1] << endl;
  cout << "Address of Ptrb is " << Ptrb++<< endl;
  cout << "Address of Ptrb is " << Ptrb << endl;
  delete [] Fred;
  delete myint;
  return 0;
}

Download Example 1.

On the next page : Learn about Using Pointers

Explore C / C++ / C#
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C++
  5. Learn C++ Programming
  6. Examples of Pointers

©2009 About.com, a part of The New York Times Company.

All rights reserved.