C++ Pointers

CoderSource.net
C++ Pointers
Rating: 2

Date: 3/19/2005 12:00:00 AM

   We can define a variable in C++ to store a memory address. A pointer in C++ is said to "point to" the memory address that is stored in it. Also, when defining a C++ pointer variable, we must specify the type of variable to which it is pointing. For example, to define a pointer, which will store a memory address at which exists an int, we can do the following: 

//Sample program for c++ pointer
signed main()
{
    int* p;
    //Right now, p contains no particular myval in this C++ code.
}

   The asterisk in the above specifies that we have a pointer variable.    Let's say we want to define an int variable and then we want to define a pointer variable, which will store the memory address of this int:

 //c++ pointer using an int variable 

signed main()
{
    int myval(7);
    int* p_myval;
    //Right now, p_myval contains no particular myval.
    p_myval = &myval;
    //Now, p_myval in this c++ program contains the memory address of the variable myval
}

    With &myval, & is referred to as "the address-of operator". The expression &myval is of the c++ type int*. We then store this int* myval in our int* variable, which is p_myval.    Now, we will actually use this pointer:    

//Sample program for c++ pointer 

signed main()
{
    int myval = 7;
    int* p_myval = &myval;
    *p_myval = 6;
}

    With *p_myval = 6, the asterisk is referred to as "the dereference operator". It turns the expression from an int* into an int. The statement has the effect of setting the myval of myval to 6.    So now what are the uses of pointers in c++? Let us see something about how and when they should be used:  

  A) When the pointer must be re-seated.

  B) When arrays are involved.

 Consider a string, an array of characters: 

signed int main()
{
    char my_name[] = "Code";
}

   Here's what the string (char c++ pointer) looks like in memory:

Name of VariableType of VariableAddress in Memory Value Stored
my_name Char 108 'C'
char 109 'o'
char110'd'
char111'e'
char112'\0'
   While accessing the characters inside the variable my_name, the position of the first character will start from 0. So the array of size 4 will be accessed for characters from 0, 1, 2 and 3. We can define a pointer to point to the second element in the array my_name, as so: 

int main()
{
    char my_name[] = "Code";
    char* k( &my_name[1] );
}

   Now, what k points to looks like so in memory:

Name of Variable Type of VariableAddress in Memory Value Stored
my_name Char* 116 109
char 109 'o'
char110'd'
char111'e'
char112'\0'
   So that's one usage of c++ pointers there, to point to an individual object in an array.    The other feature of c++ pointers is that they can be "re-seated", which means that you can change their value, you can change what they're pointing to, as in the following: // c++ pointer program for modifying values/re-seating. 

signed int main()
{
    int myval(5);
    int myvalue2 = 7;
    int* p_primate;
    p_primate = &myval;
    *p_primate = 9;
    p_primate = &myvalue2;
    *p_primate = 10;
}

   Guess what kind of variable we have in the following: 

signed main()
{
    signed** p_p_cow;
}

    An int* c++ pointer points to an int, so an int** points to an int*. In English: The variable p_cow above stores a memory address. At that memory address exists a variable of type int*. This int* variable also stores a memory address, at which exists an int. Take the following: 

//Snippet for c++ pointer to pointers 

int main()
{
    int cow(7);
    int* p_cow = &cow;
    int** p_p_cow(&p_cow);
    int*** p_p_p_cow = &p_p_cow;
}

    Here's what the above c++ pointers look like in memory:

Name of VariableType of VariableAddress in Memory Value Stored
Cow Int 108 7
p_cow int* 110 108
p_p_cow int** 112 110
p_p_p_cow int*** 114 112
   With the above code, we can set the value of cow using p_p_p_cow: 

//Using c++ pointer to pointer 

int main()
{
    int cow(7);
    int* p_cow = &cow;
    int** p_p_cow(&p_cow);
    int*** p_p_p_cow = &p_p_cow;
    ***p_p_p_cow = 8;
}

    C++ Pointers are commonly used when working with strings. Let's define a function; this function will be supplied with a string. We're going to change the 2nd, 5th and 7th characters of the string: 

void ChangeString(char* const p_first_char)
{
    p_first_char[1] = 'a';
    p_first_char[4] = 'b';
    p_first_char[6] = 'c';
}

    Or we can define a function, which will be supplied with a string. The function will return the first instance of the character 't' in the string: 

char* GetFirstT(char* p_first_char)
{

for ( ; *p ; ++p)
{
if ( *p == 't' ) return p;
}
return 0;

signed main()
{

char the_alphabet[] = "abcdefghijklmnopqrstuvwxyz";
char* p_t = GetFirstT(the_alphabet);

}


   Now I'm going to talk about c++ pointers and constness. If you want a const c++ pointer variable, a c++ pointer variable whose value you can't change after initialization, then stick the const directly beside the variable's name: 

signed main()
{

int myval = 5;
int myvalue2(8);
int* const p_k = &myval;
p_k = &myvalue2; //COMPILE ERROR
*p_k = 3; //No problem, the variable to which it points is non-const

}

   If you want a non-const c++ pointer variable, but you want the variable to which it points to be const, then: 

signed main()
{

int myval(7);
int myvalue2 = 6;
const int* p_k = &myval;
p_k = &myvalue2; //No problem, the variable is non-const
*p_k = 7; //COMPILE ERROR

}

   If you want a const c++ pointer that points to a const variable then: 

signed int main()
{

int myval(17);
int myvalue2 = 4;
const int* const p_k = &myval;
p_k = &myvalue2; //COMPILE ERROR
*p_k = 32; //COMPILE ERROR

}

You Can Rate this Article, if you are Logged In      
 

More Links from CoderSource.net:

 
Refer to a Friend:

Your Details:

Name:     e-mail:

Friend Details:

Name:    e-mail:    


MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Add to Google
Add to My Yahoo!
Welcome to Codersource.Net Login | Register | Faq  

SEARCH
Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us