Showing posts with label Pointer. Show all posts
Showing posts with label Pointer. Show all posts

Monday, September 5, 2011

this pointer in C++ Programs


Normally pointers can hold the address of it’s own type of variable. this is a keyword used to denote a pointer which can hold the address of any type of location. In C++ programs, this pointers are used to hold the address of objects of any class.

#include< iostream.h >
#include< conio.h >
class Exam
{
private:
int i;
public:
void setData (int a)
{
this->i=a;
cout<< "Address of the object is: -"<< this;
}
void show ()
{
cout<< "\nData member="<< this->i;
}
};
void main ()
{
clrscr ();
Exam s1, s2;
int i;
cout<< "\nEnter value for 1st object: -";
cin>>i;
s1.setData (i);
s1.show ();
cout<< "\nEnter value for 2nd object: -";
cin>>i;
s2.setData (i);
s2.show ();
getch ();
}


Wednesday, August 31, 2011

Reference to a pointer in C++ programs


We can create reference to a pointer also in C++ programs. The declaration of such pointer would look like:

char *pstr=”Hello”;
char *&p=pstr;

In the following C++ program a pointer will hold the base address of a string and another reference to pointer variable will hold the same value. Using the pointer the string will be converted into upper case letters. The same effect will be observed in the reference pointer.

C++ program to demonstrate the use of reference pointer

#include<iostream.h>
#include<conio.h>
#include<ctype.h>
void up(char *p)
{
while(*p!=NULL)
{
*p= toupper(*p);
p++;
}
}
void main()
{
clrscr();
char *pstr;
cout<< "Enter any word:";
cin>>pstr;
char *&p=pstr;
cout<< endl<< "String through the pinter:"<< pstr;
cout<< endl<< "String through the reference pointer:"<< p;
up(pstr);
cout<< endl<< "String through the pinter(after modification):"<< pstr;
cout<< endl<<"String through the reference pointer(after nodification):"<< p;
getch();
}

Though an array of pointers is acceptable in C++ programs>, an array of references is not.


void pointer in C++ Programs


Normally we know that a pointer can hold the address of any location provided the types of both the location and the pointer are same. But there is an exception. There is a general purpose pointer in C++ called void pointer. Void pointer can hold the address of any type of location and it can transfer the address to another pointer of the location type.

Here is an example showing the use of void pointer in C++ programs.


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char ch[100];
char *ptr;
void *p;
cout<< "\nEnter a string:";
cin.get(ch,100);
p=ch;
ptr=(char )p;
cout<< endl<< "Now string display through the 'char' pointer:"<< ptr;
getch();
}

Another example showing the use of void pointer in C++ programs.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
int *ptr;
void *p;
cout<< "\nEnter a number:";
cin>>num;
p=&num;
cout<< endl<< "Address of the variable through void pointer:"<< p;
ptr=(int *)p;
cout<< endl<< "Value display through the 'int' pointer:"<< *ptr;
cout<< endl<< "Address display through the 'int' pointer:"<< ptr;
getch();
}

Through void pointer we can not display the content of a location. Such pointers have some specialized uses C++, such as passing pointers to functions that operate independently of the data type pointed to.

Tuesday, February 15, 2011

Use of pointer variables as structure member in C programming


So far we have used normal variables and arrays as structure item or variable. Now we will see how to use pointer variables as structure member.

The following program illustrates the use of pointers in structure.

# include< string.h>
# include< stdio.h>
# include< stdlib.h>
struct Record
{
            char *name;
            char *address;
            long pin;
};

void sort(struct Record *str, int n)
{
            int i,j;
            struct Record temp;
            for(i =0; i <n; i++)
            {
              for (j=i+1; j <n; j++)
              {
               if (strcmp (str[i].name, str[j].name)>0)
               {
                        temp = str[i];
                        str[i] = str[j];
                        str[j] = temp;
               }
              }
             }
}

void main()
{
            char choice;
            int i, n = 0;
            struct Record string[10];
            clrscr();
            printf("Input The List\n ");
            printf("\n**************\n");

do
{
 string[n].name = (char *) malloc(50*sizeof(char));
 string[n].address= (char *) malloc(100*sizeof(char));
printf("Name: ");
gets(string[n].name);
printf("\n Address: ");
gets(string[n].address);
printf("Pin Code: ");
scanf("%ld", &string[n].pin);
n++;
fflush(stdin);
printf("\n Input another y/n? ");
choice = getchar();
fflush(stdin);
}while(choice == 'y');
  printf("Unsorted list \n");
printf("\n********************\n");
for (i =0; i < n ; i++)
{
printf("\n %s", string[i].name);
printf("\n %s", string[i].address);
printf("\n %ld", string[i].pin);
printf("\n********************\n");
}
getch();
sort(string, n);
printf("\n Sorted list");
printf("\n********************\n");
for (i =0; i < n ; i++)
{
printf("\n %s", string[i].name);
printf("\n %s", string[i].address);
printf("\n %ld", string[i].pin);
printf("\n********************\n");
}
getch();
}

About the program

Name, address and pin number of several persons are stored in the structure type array.  In structure definition two char type pointers are used to store name and address. Locations to store name and address will be created dynamically.
The structure type array is ultimately sored according to names in ascending order and records are displayed.



Thursday, December 2, 2010

Use of Pointer in Structure in C programming


C program to illustrates the use of pointers in structure

# include< string.h>
# include< stdio.h>
# include< stdlib.h>
struct Record
{
            char *name;
            char *address;
            long pin;
};
/* This function will be used to sort the list according to name. structure type array variable will be created and address of the  first location and the number of locations, means records will  be passed as argument.*/
void sort(struct Record *str, int n)
{
            int i,j;
            struct Record temp;
            for(i =0; i <n; i++)
            {
              for (j=i+1; j <n; j++)
              {
               if (strcmp (str[i].name, str[j].name)>0)
               {
                        temp = str[i];
                        str[i] = str[j];
                        str[j] = temp;
               }
              }
             }
}
void main()
{
            char choice;
            int i, n = 0;
            struct Record string[10];
            clrscr();
            printf("Input The List\n ");
            printf("\n**************\n");

do
{
 string[n].name = (char *) malloc(50*sizeof(char));
 string[n].address= (char *) malloc(100*sizeof(char));
printf("Name: ");
gets(string[n].name);
printf("\n Address: ");
gets(string[n].address);
printf("Pin Code: ");
scanf("%ld", &string[n].pin);
n++;
fflush(stdin);
printf("\n Input another y/n? ");
choice = getchar();
fflush(stdin);
}while(choice == 'y');
  printf("Unsorted list \n");
printf("\n********************\n");
for (i =0; i < n ; i++)
{
printf("\n %s", string[i].name);
printf("\n %s", string[i].address);
printf("\n %ld", string[i].pin);
printf("\n********************\n");
}
getch();
sort(string, n);
printf("\n Sorted list");
printf("\n********************\n");
for (i =0; i < n ; i++)
{
printf("\n %s", string[i].name);
printf("\n %s", string[i].address);
printf("\n %ld", string[i].pin);
printf("\n********************\n");
}
getch();
}

Friday, November 5, 2010

Passing array to function in C Language

In the next example we will store some values in an array and the address of each location of the array will be passed to a function one after another . The function will display the value stored in the location.

#include< stdio.h>
 void dis(int *);
void main()
{
 int i;
 int arr[]={3,4,5,6};
 for(i=0;i< 4;i++)
 dis(&arr[i]);
 }
 void dis(int *p)
 {
 printf("\nValue=%d",*p);
}

Now the same program is modified so that  the entire array is passed to the function at a time.

#include<stdio.h>
 void dis(int *,int);
void main()
{
  int arr[]={3,4,5,6};
  dis(&arr[0],4);
 }
 void dis(int *p,int no)
 {
 int i;
 for(i=0;i< no;i++)
 {
 printf("\nValue=%d",*p);
 p++;
 }
}


Pointer to pointer in C programming language


The concept of pointer can be extended further. Pointer can hold the address of another variable. But what will happen if the variable itself is a pointer. The address of a pointer can be stored on a variable which is called pointer to pointer.

#include< stdio.h >
void main()
{
 int i,*j,**k;
 /* here j is a pointer and k is pointer to pointer*/
 i=5;
 j=&i;
 k=&j;
  printf("\nAddress of 'i'=%u",&i);
  printf("\nAddress of 'i'(displayed through 'j')=%u",j);
  printf("\nAddress of 'i'(displayed through 'k')=%u",*k);
  printf("\nAddress of 'j'=%u",&j);
  printf("\nAddress of 'j'(displayed through 'k')=%u",k);
  printf("\nAddress of 'k'=%u",&k);
  printf("\nNow the values...\n");
  printf("\nValue of 'j'( this will be the address of 'i')=%u",j);
  printf("\nValue of 'k' (this will be the address of 'j')=%u",k);
  printf("\nValue of 'i' displayed through 'j'=%d",*j);
  printf("\nValue of 'i' displayed through 'k'=%d",**k);
 }

A chart will demonstrate this feature
Location
 Name       i          j          k
Value      5       1002     1000       
Address 1002  1000      998

Difference between *ptr++ and ++*ptr  (assume ptr is a pointer and pointing to some location)
*ptr++ statement increments the pointer means the pointer moves to the next location while ++*ptr statement increments the value of the location pointed by the pointer by 1.
++*ptr statement can be also written as (*ptr)++

Another example using C Language

#include< stdio.h >
void main()
{
 int x,*y;
 void *z;
 x=10;
 z=&x;
 y=(int *)z;
printf("\nAddress of 'x'=%u",&x);
printf("\nAddress of 'x'through 'y'=%u",y);
printf("\nAddress of 'x' through 'z'=%u",z);
}
 all the three statements will generate the same output, address of the variable ‘x’


Monday, October 4, 2010

Pointer and character type array


Like integer array pointer can also points to character array.
Here is a sample C program :

#include< stdio.h >
void main ()
{
char str []="This is a string";
char *str2=str;
clrscr ();
printf ("%s\n", str);
printf ("%s\n", str2);
str2++;
printf ("%s\n", str2);
 getch ();
 }



Another such program using C Language

#include< stdio.h >
void main ()
{
char str [20];
char *str2;
clrscr ();
printf ("Enter any string:-");
scanf ("%s", str);
str2=str;
printf ("String displayed through the variable:-%s\n", str);
printf ("String displayed through the pointer:-%s\n", str2);
printf ("\nString displayed character wise through the pointer:-\n");
while (*str2!='\0')
{
printf ("%c\n", *str2);
str2++;
}
 getch ();
 }


Monday, September 13, 2010

Accessing array using pointer in c programming language

Program on accessing array using pointer


In this program we will accept five numbers from the user and store them in an array and the values will be displayed through pointer.

#include< stdio.h >
void main ()
{
int num [5];
int i, *ptr;
clrscr ();
printf ("Enter the five numbers:-");
for (i=0;i< 5;i++)
{
scanf ("%d", &num [i]);
}
ptr=num;
printf ("\nThe numbers are:-");
for (i=0;i< 5;i++)
printf ("%d ", *ptr++);
getch ();
 }


Friday, August 27, 2010

Array and pointers in C programming language


When an array is declared in C language, compiler allocates a base address and sufficient amount of storage to hold the elements. The base address is the location of the first element (index 0) .The compiler also defines the array name as a constant pointer to the first element. Suppose we declare an array as follows:
                                   int arr [4] = [1,2,3,4};

The array name arr is defined as a pointer pointing to the first element arr [0] and therefore the value of arr is 1000(say),the location where arr [0] is stored. arr = &arr [0] = 1000

The address of the second element can be written as either &arr [1] or arr+1.Here the expression arr+1 represents an address rather than an arithmetic expression. Now if we declare ptr as a pointer variable and want to point the array arr, then the statement looks like: ptr = arr;This is equivalent to ptr = &arr [0];
We can access every value of arr using ptr++ to move from one element to another.*(ptr+2) gives the value of arr [2]. The pointer accessing method is much faster than array indexing.

Here is a C Program to demonstrate the above feature


#include< stdio.h>
void main ()
{
int *ptr, sum, i;
int arr [4]={1,2,3,4};
clrscr ();
i=0;
sum=0;
ptr=arr;
printf ("Element   Value     Address\n");
while (i< 4)
{
 printf ("arr [%d]    %d         %u\n", i, *ptr, ptr);
 sum=sum+*ptr++;
/*note ‘postfix’ operator.*/
 i++;
  }
 printf ("\nSum is:-%d", sum);
 getch ();
 }

Monday, August 23, 2010

Pointers in C Language

What is pointer


Pointer is the flavor of C Language. It’s a special type of variable that can hold the address of same type of variable. So we can say that variables deals with value while pointers deals with address in C Language.

Consider the following declaration:-
int i =50;
This declaration tells the C compiler to-
Reserve space in memory to store an integer value
Associate the name i  with the memory location
store the value 5 at this location

This feature can be demonstrate with the help of a chart
 Location Name     i
 Value                    50
Address                1000

In C Language, &is called address operator while * is called value at address  or indirection operator.

C program to illustrate the use of pointer

#include< stdio.h >
 void main()
 {
  int i=5;
  printf("\nValue stored is=%d",i);
  printf("\nAddress of the location is=%u",&i);
  printf("\nValue stored is=%d",*(&i));
  }


From the above program it is clear that &i returns the address of i.So, this address can be stored in a variable and address of a variable can be stored in pointer.

The above C  program on pointeris modified as follows:-

#include< stdio.h >
 void main()
 {
  int i=5;
  int *j;
  j=&i;
  printf("\nvalue stored in the pointer(address of the variable'i')is=%u",j);
  printf("\nAddress of the location (through address operator)is=%u",&i);
  printf("\nValue stored (displayed through pointer)is=%d",*j);
  printf("\nValue stored (displayed through variable)is=%d",i);
  printf("\nAddress of the pointer is=%u",&j);
  }




Subscribe via email

Enter your email address:

Delivered by FeedBurner