Pointer is a variable that points to or references a memory location in which data is stored. Each memory cell in the computer has an address that can be used to access that location so a pointer variable points to a memory location we can access and change the contents of this memory location via the pointer.
A pointer is a variable that contains the memory location of another variable. The syntax is as shown below. You start by specifying the type of data stored in the location identified by the pointer. The asterisk tells the compiler that you are creating a pointer variable. Finally you give the name of the variable.
type * variable name
//Example: int *ptr ; float *string ;
Hera * is the dereference operator
# include <iostream.h>
# include <conio.h>
void main ()
{
int *ptr ;
int sum ;
sum = 39 ;
ptr = ?
Cout << sum ;
Cout << ptr ;
}
Once we declare a pointer variable we must point it to something we can do this by assigning to the pointer the address of the variable you want to point as in the following eg
ptr = &num ;
& is the address of operator.
This places the address where num is stores into the variable ptr. If num is stored in memory 21260 address then the variable ptr has the value 21260.
# include <iostream.h>
# include <conio.h>
int main()
{
int i = 9 ;
Cout << i ;
Cout << &i ; /* Print the memory address */
Cout << *(&i) ; /* It will give the value */
return (0) ;
}
# include <iostream.h>
# include <conio.h>
void main ()
{
int num, *intptr ;
float x , *floptr ;
char ch , *cptr ;
num = 123 ;
x = 12.34 ;
ch = 誕' ;
intptr = &x ;
cptr = &ch ;
floptr = &x ;
Cout << 哲um stored at address:? << *intptr << intptr ;
Cout << 天alue stored at address:? << *floptr << floptr ;
Cout << 鼎haracter stored at address:? << *cptr << cptr ;
}
Only addition and subtraction can be done in pointers and no other operations can be performed.
# include <iostream.h>
# include <conio.h>
void main ()
{
int ptr1 , ptr2;
int a ,b ,x ,y ,z ;
a = 40 ; b = 6;
ptr1 = &a ;
ptr2 = &b ;
x = *ptr1 + *ptr2 ? 6 ;
y = 6* - *ptr1 / *ptr2 + 40 ;
Cout << 鄭ddress of a:? << ptr1 ;
Cout << 鄭ddress of b:? << ptr2 ;
Cout << a << b ;
Cout << x << y ;
ptr1= ptr1 + 60 ;
ptr2 = ptr2 ;
Cout << a << b ;
}
The pointers are very much used in function declaration. Sometimes only with a pointer a complex function can be easily represented and success. The usage of pointer in a function definition may be classified into two groups:
# include <iostream.h>
# include <conio.h>
void swap (int x , int y)
{
int t ;
t = x ;
x = y ;
y = t ;
}
int main ()
{
int a = 20 , b = 22 ;
swap (a , b) ;
Cout << a << b ;
}
# include <iostream.h>
# include <conio.h>
void swap (int &x , int &y)
{
int t ;
t = x ;
x = y ;
y = t ;
}
int main ()
{
int a = 20 , b = 22 ;
swap (a , b) ;
Cout << a << b ;
}
An array is actually very much like pointer. We can declare the arrays first element as a[0] or as int *a because a[0] is an address and *a is also an address the form of declaration is equivalent. The difference is pointer is a variable and can appear on the left of the assignment operator that is lvalue. The array name is constant and cannot appear as the left side of assignment operator.
# include <iostream.h>
# include <conio.h>
void main ()
{
int a [60] ;
int i , j , n ;
Cout << "Enter the elements of the array" ;
Cin >> n ;
Cout << "Enter the array elements" ;
for (I = 0 ; I < n ; I++)
Cin >> a[I] ;
Cout << "Array element are" ;
for( ptr = a , ptr < (a+n) ; ptr++)
Cout << *ptr , ptr ;
}
Strings are characters arrays and here last element is 0円 arrays and pointers to char arrays can be used to perform a number of string functions.
(追記) (追記ここまで)Others
Languages
Frameworks
Web / Design
Mobile Technology
Sql & Technology
R4R