C Introduction to Programming Environments C UNIX IPE

Document Sample
C Introduction to Programming Environments C UNIX IPE Powered By Docstoc
					Introduction to Programming
 Environments (C++/UNIX)


         IPE 115, Semester 1, 2009

                        Lecture 08
                          Pointers
                  Introduction
• A pointer is a variable that holds a memory address.
• Why pointers?
   – Provide the means by which functions can modify the
     arguments of the calling function.
     Note: pass by reference was not available in C.
   – Pointers support dynamic allocation of memory.
   – Can improve the efficiency of some routines.
            Declaring a Pointer
                             Memory
Syntax:              Address      Contents
                  m 0x3267A1B0      0x32
type *name;         0x3267A1B1       67
                    0x3267A1B2       A1
                    0x3267A1B3       B8
Examples:           0x3267A1B4
int *m;             0x3267A1B5
char *array;        0x3267A1B6

double *temp;       0x3267A1B7
                    0x3267A1B8
                    0x3267A1B9
int **matrix;
                    0x3267A1BA
                    0x3267A1BB
                Pointer Operators
• Once a pointer is declared, the operator * can
  be used to obtain the value located at the
  address that is held by the pointer.
  – Eg : cout<< *m ; // will print the value at the location pointed by m.

• The operator & can be used to obtain the
  memory address of an operand.
  – Eg : cout << &a; // will print the memory address of a.

• Both the operators, * and & are unary
  operators. That is, it uses only one operand.
                   Pointer Operators
                                                 Memory
                                         Address      Contents
Declaring a pointer                  m 0x3267A1B0      0x32

• Example:                             0x3267A1B1       67
                                       0x3267A1B2       A1
                                       0x3267A1B3       B8
        int *m;
                                       0x3267A1B4
                                       0x3267A1B5
                                       0x3267A1B6
                                       0x3267A1B7
Note :                                 0x3267A1B8
  Initially the pointer will point     0x3267A1B9
  at ( will store the memory           0x3267A1BA
  address of ) any location in the     0x3267A1BB
  memory.
                    Pointer Operators
                                                        Memory
                                                Address      Contents
• Example:                                  m 0x3267A1B0      0x32
int *m;                                       0x3267A1B1       67
int count = 7;                                0x3267A1B2       A1
                                              0x3267A1B3       B8
// count is a variable that stores an   count 0x3267A1B4       0
    integer.                                  0x3267A1B5       0
                                              0x3267A1B6       0
                                              0x3267A1B7       7
                                              0x3267A1B8
                                              0x3267A1B9
                                              0x3267A1BA
                                              0x3267A1BB
                    Pointer Operators
                                                   Memory
                                           Address      Contents
• Example:                             m 0x3267A1B0      0x32
int *m;                                  0x3267A1B1       67
int count = 7;                           0x3267A1B2       A1
                                         0x3267A1B3       B4
m = &count;                        count 0x3267A1B4       0

// Assigns the memory address of         0x3267A1B5       0

   count to the pointer m                0x3267A1B6       0
                                         0x3267A1B7       7
                                         0x3267A1B8
                                         0x3267A1B9
• The value of m is 0x3267A1B4           0x3267A1BA
                                         0x3267A1BB
• The value of *m is       7
         Pointers and Functions
/* C program */               //C++: pass by reference
void main(void)               int main()
{                             {
   int x=4, y=7;                 int x=4, y=7;
   mySwap(&x, &y);               mySwap(x, y);
} /* main */                  } //main

void mySwap(int *a, int *b)   void mySwap(int &a, int &b)
{                             {
   int temp;                     int temp;
   temp = *a;                    temp = a;
   *a = *b;                      a = b;
   *b = temp;                    b = temp;
} /* mySwap */                } //mySwap
            Arrays and Pointers
                                      name 0x3267A1B0   0x32
• An array name is a pointer.              0x3267A1B1    67
• Example:                                 0x3267A1B2   A1
  //here the variable “name” is a          0x3267A1B3   B5

  //pointer.                               0x3267A1B4
                                           0x3267A1B5   ‘h’
  char name[20] = “hello”;
                                           0x3267A1B6    ‘e’
                                           0x3267A1B7    ‘l’
                                           0x3267A1B8    ‘l’
• In C, to read a character we used        0x3267A1B9   ‘o’
                                           0x3267A1BA   ‘\0’
  scanf(“%c”, &ch);
                                           0x3267A1BB
• To read a string we need to use
  scanf(“%s”, name); //no & since “name is a pointer
    Arrays and Pointer Arithmetic
                              arr   0x3267A1B0   0x32   0x3267A1C0

• Example                           0x3267A1B1
                                    0x3267A1B2
                                                 67
                                                 A1
                                                        0x3267A1C1
                                                        0x3267A1C2
                                    0x3267A1B3   B8     0x3267A1C3




                                                                     4.6
                              ptr   0x3267A1B4   0x32   0x3267A1C4
                                    0x3267A1B5   67     0x3267A1C5
double arr[3] = {1.3, 4.6, 0.2};    0x3267A1B6   A1     0x3267A1C6
double *ptr;                        0x3267A1B7   B8     0x3267A1C7
ptr = arr;                          0x3267A1B8          0x3267A1C8
                                    0x3267A1B9          0x3267A1C9
cout << *ptr; //prints 1.3
                                    0x3267A1BA          0x3267A1CA
ptr++;




                                                  1.3
                                    0x3267A1BB          0x3267A1CB




                                                                     0.2
cout << *ptr; //prints 4.6          0x3267A1BC          0x3267A1CC
ptr++;                              0x3267A1BD          0x3267A1CD
cout << *ptr; //prints 0.2          0x3267A1BE          0x3267A1CE
                                    0x3267A1BF          0x3267A1CF
           Multiple Indirection
• Allows a pointer point to another pointer that
  points to a target value.
• Also called pointers to pointers.
                   Pointer             Variable
                   address              value

                        Single Indirection

         Pointer             Pointer              Variable
         address             address               value

                       Multiple Indirection
               Dynamic Allocation
• Allows a program to obtain memory at
  runtime.
• Dynamic Allocation Functions in C
  – malloc() is used to allocate memory.
  – free() is used to release memory.
• Dynamic Allocation Functions in C++
  – new is used to allocate memory.
  – delete is used to release memory.
  Note: More dynamic allocation functions are available in C/C++.
       Examples of Dynamic Allocation
              ( C Language )
/*Example 1*/
int *p;
p = (int *) malloc(1000); /* initializes an integer to 1000 */
if (!p){
   cout << “Cannot Allocate Memory” << endl;
   exit(1);
}
free(p);


/*Example 2*/
int *k;
k = (int *) malloc(1000*sizeof(int)); /* An array of 1000 integers */
if (!k){
   cout << “Cannot Allocate Memory” << endl;
   exit(1);
}
free(k);
   Examples of Dynamic Allocation
//Example 3
              ( C ++ )
int *p;
p = new int (87); //Initializes to 87. That is *p is 87
if (!p){
   cout << “Cannot Allocate Memory” << endl;
   exit(1);
} //if
delete p;

//Example 4
int *k;
k = new int [10]; //an array of 10 integers
if (!k){
   cout << “Cannot Allocate Memory” << endl;
   exit(1);
} //if
delete [] k;

Note: the new operator does not require type casting and sizeof()
       Comments about Pointers
• Pointers must be initialized. A common error
  is shown below.
//This program is wrong
int main()
{
   int x, *p; //p can point to any memory location

   x = 10;
   *p = x;     //The value of x will be written to some
   return 0;   //                    unknown location
} //main

• Lack of understanding of pointers can lead to
  serious errors.

				
DOCUMENT INFO
Shared By:
Stats:
views:49
posted:5/21/2011
language:English
pages:15
Description: This document is about C++ pointers