728x90 AdSpace

Latest Article

Pointer in C++ Example Program Exercises with Source Code




  • What is a pointer?
  • What is pointer arithmetic?
  • Use of pointers for copying strings
  • Pointer arithmetic is the same thing as array indexing?
  • Pointers and Structures
  • Array of Strings using Pointers
  • Pointers and dynamic allocation of memory
Things to look into after the lab:
  • Pointers to functions


What is a Pointer?

------------ Program 1.1 --------------------------------- 
 
#include <iostream.h>
 
int j, k;
int *ptr;
 
int main(void)
{
    j = 1;
    k = 2;
    ptr = &k;
    cout<<"\n";
    cout<<"j has the value "<<j <<" and is stored at "<< (void *) &j;
    cout<<endl;
    cout<<"k has the value "<<k <<" and is stored at "<< (void *) &k;
    cout<<endl;
 
    cout<<"ptr has the value "<<ptr <<" and is stored at "<< (void *) &ptr;
        cout<<endl;
    cout<<"The value of the integer pointed to by ptr is "<<*ptr;
        cout<<endl;
 
    return 0;
}-------------- end of program 1.1 --------------







What is Pointer Arithmetic?

-----------  Program 1.2  -----------------------------------

#include < iostream.h>
 
int my_array[] = {1,23,17,4,-5,100};
int *ptr;
 
int main(void)
{
    int i;
    ptr = &my_array[0];     /* point our pointer to the first
                                      element of the array */
    cout<<"\n\n";
    for (i = 0; i < 6; i++)
    {
               cout<<"my_array["<<i<<"] = " <<my_array[i];   /*<-- A */
               cout<<"\n";
               cout<<"ptr + "<< i << "= "<< *(ptr + i);        /*<-- B */
               cout<<"\n";
    }
    return 0;
}
-------------- end of program 1.2 --------------

 
 

 




Various ways of writing a string copy function:

-----------  Program 1.3  -----------------------------------

char *my_strcpy(char *destination, char *source)
{
    char *p = destination;
    while (*source != '\0')
    {
        *p++ = *source++;
    }
    *p = '\0';
    return destination;
-------------- end of program 1.3 --------------

-----------  Program 1.4  -----------------------------------

char *my_strcpy(char dest[], char source[])
{
     int i = 0;
     while (source[i] != '\0')
     {
         dest[i] = source[i];
         i++;
     }
     dest[i] = '\0';
     return dest;
}
-------------- end of program 1.4 --------------

 
 





Pointers and Structures:
--------------- program 1.5 ------------------
 
#include <iostream.h>
#include <string.h>
 
struct tag {
    char lname[20];      /* last name */
    char fname[20];      /* first name */
    int age;             /* age */
    float rate;          /* e.g. 12.75 per hour */
};
 
struct tag my_struct;       /* declare the structure my_struct */
 
void fill(tag *my_struct)
{
    strcpy(my_struct->lname,"Jensen");
    strcpy(my_struct->fname,"Ted");
}
int main(void)
{
    fill(&my_struct);
    cout<<"\n"<< my_struct.fname;
    cout<<"\n"<< my_struct.lname;
    return 0;
}

 
 



Array of Strings using Pointers:
------------------- program 1.6 ----------------------
 
#include <iostream.h>
#define ROWS 5
#define COLS 10
 
int multi[ROWS][COLS];
 
int main(void)
{
    int row, col;
    for (row = 0; row < ROWS; row++)
    {
        for (col = 0; col < COLS; col++)
        {
            multi[row][col] = row*col;
        }
    }
 
    for (row = 0; row < ROWS; row++)
    {
        for (col = 0; col < COLS; col++)
        {
            cout<<"\n"<<multi[row][col];
            cout<<*(*(multi + row) + col);
        }
    }
 
    return 0;
}
----------------- end of program 1.6 ---------------------   
 
 




Pointers and dynamic allocation of memory:

--------------- Program 1.7 --------------------------------
 
#include <iostream.h>
 
 
int main(void)
{
    int nrows = 3; 
    int *ptr;
    ptr = new int[nrows];
    for (int row = 0; row < nrows; row++)
    {
        ptr[row] = 17;
        cout<<ptr[row];
    }
 
    return 0;
}
------------- End of Prog. 1.7 --------------------------------
 
 



Exercises:
  • Write a program using pointers which swaps two numbers.
  • Write a program that reads in a table of numbers and finds out the average of each row and column. This program should first take two numbers as number of rows and columns as input from the user.
  • Dynamically allocate an array of student structure which takes as input the number of student records to be entered. Student structure should be like this

struct student
{
     char *RegNo;
     char *Name;
}

Pass this structure to a function which takes input from the command prompt and fills in the structure
no image





  • Blogger Comments
  • Facebook Comments

0 comments:

Post a Comment

Top