Pointers

Pointers

int *ptr ;
* inform compiler that ptr is a pointer variable.  int is the type of data stored at the address.
pointer is said to “point to” an integer, says we intend to use our pointer variable to store the address of an integer.

ptr = NULL ;  to guarantee that the pointer is a null pointer… as compilers are not all the same.

ptr = &k ;   ptr point to k…take the address of k
*ptr=7 ;      will copy 7 to the address pointed by ptr, will set the value of k to 7.

could be simply written int *ptr = &k ;

.
int *a ;
int *p = a ;   //  equivalent to   :     int *p ;   p = a ;

.

int a ;
int  *p = &a ;   // equivalent to :    int *p ;    p = &a ;

.

ptr +1 ;
Because the compiler knows this is a pointer (i.e. the value is an address ) and that it points to an integer (let´s say its current address 100, is the address of an integer), it adds 2 to ptr instead of 1, so the pointer “points to” the next integer, at memory location 102.
It would add 4 if ptr declared as a pointer to a long.


Pointers and Arrays

int my_array[] = { 1 , 23 , -17 } ;

is the same as :
int my_array[0]= 1  ;
int my_array[1]= 23 ;
int my_array[2]= -17 ;

int balance [10] ;
balance is a pointer to &balance[0], which is the address of the first element of the array balance.
Thus, the following program fragment assigns p the address of the first element of balance:

int *p ;
int balance [10] ;
p
= balance ;     // alternatively we could write p = &balance[0] ;

Pointers and arrays are strongly related.
An array name is a constant pointer to the first element of the array :  p = balance ;
balance is short for &balance[0], which is the address of first element.


Pointers and Strings

In C, strings are arrays of characteres terminated with a binary zero charactere ( written as ‘\O’,  slash + zero)

char my_string[40] = { ‘T’ , ‘e’ , ‘d’ , ‘\O’ , } ;

char my_string[40] = “Ted” ;     the charactere ‘\O’ is automatically appended to the end of the string.
we allocate space for 40 bytes array and put the string in the first 4 bytes, three for the characters in the quote and a 4th to handle the terminating ‘\O’.

Actually if all we wanted to do was to store the name “Ted” we could write : char my_string[] = “Ted” ;
and the compiler wwould count characters, leave room for nul character and store the total of the four characters in memory.

an alternative approach  char *my_string = “Ted” ;
using the array notation 4 bytes of storage in the static memory block are taken
.
In the pointer notation the same 4 bytes requires, plus N bytes to store the pointer variable my_string ( where N depends on the system but it is usually 2 bytes and can be 4 or more ).

#include
char strA[80] = “an example” ;
char *pA ;
pA = strA ;            // we copy the address of strA[0] into our variable pA
puts (strA) ;          // prototype function  int puts (const char *s);   the parameter passed to puts is a pointer ,
            
putchar (‘\n’) ;    // move down one line in the screen

the const used as parameter modifier informs the user that the function will not modify the string pointed to to by s, ie, it will threat that string as a constant.


void my function_A (char *ptr)
{
char a[] = “ABCDE” ;
.
.
}

The contents or value(s) of the array a[] is considered to be data. The array is said to be initialized to the value ABCDE.
The definition char a[] is a local variable and thus the string ABCDE is stored on the stack

void my function_B (char *ptr)
{
char *cp = “FGHIJ” ;
.
.
}

In this case, the value of cp is considered to be the data. The pointer has been initialized to point to the string FGHIJ.
The value of cp is a local variable and thus it is stored on the stack. The string FGHIJ can be stored anywhere ( data segment maybe !)


When we declare a variable we set aside a spot in memory to hold the value of the appropriate type

Example 1

#include
using namespace std;

int main()

{

int var1;

char var2 [10] ;

cout << “Address of var1 variable:;

cout << &var1 << endl ;

cout << “Address of var2 variable: ;

cout << &var2 << endl ;


return
0 ;

}

Result :
Address of var1 variable : 0xbfebd5c0
Address of var2 variable : 0xbfebd5b6


Example 2

#include
using namespace std ;


int
main ()

{

int var = 20 // actual variable declaration.

int *ip ;           // pointer variable

ip = &var ;     // store address of var in pointer variable

cout << “Value of var variable:;

cout << var << endl ;


//
print the address stored in ip pointer variable

cout <<“Address stored in ip variable:;

cout << ip << endl ;


//
access the value at the address available in pointer

cout << “Value of *ip variable:;

cout << *ip << endl ;


return
0 ;

}


Result :

Value of var variable : 20
Address stored in ip variable : 0xbfc601ac
Value of *ip variable : 20


Tips and Tricks

(*ptr)++ ; we are not incrementing the pointer, but that which the pointer points to

.

while (*source != ‘\O’) could speed up replacing by while (*source)

.


Functions

char *my_strcpy ( char *destination, char *source )

The function is designed to accept the values of two character pointers, i.e. addresses,
and the function, should return also an address… declared char *my_strcpy

.

char *my_strcpy ( char destination[], char source[] )

What actually gets passed is the address of the first element of each array.

.

When we pass an integer we make a copy of the integer,i.e. gets its value and put it on the stack. Within the function any manipulation of the value passed can in no way effect the original integer. But, with arrays and pointers we can pass the address of the variable and hence manipulate the values of the original variables.


 Pointers and Structures

struct tag {
                     char name[20];
int age ;

                   };

struct tag my_struct ;
my_struct.name = “Jose”;
my_stuct.age = 42 ;

In systems using stacks, this is done by pushing the contents of the structure on the stack. With large structures this could prove to be a problem. However, passing a pointer uses a minimum amount of stack spaces.

struct tag *st_ptr ;

st_ptr = &my_struct ;

we could write :

(*str_ptr).age = 42 ;

Designers of C have created an alternate syntax with the same meaning which is :

st_ptr -> age = 42 ;


Reference

Pointers by Ted Jensen    PDF file

http://www.tutorialspoint.com/cplusplus/cpp_pointers.htm

http://www.learncpp.com/

.

.

.

.

.

Leave a comment