You are on page 1of 18

Pointers

Pointer
 Pointers are variables that contain memory
addresses as their values.
 A variable name directly references a value.
 A pointer indirectly references a value. Referencing
a value through a pointer is called indirection.
 A pointer variable must be declared before it can be
used.
Pointer
ADDR1 Contents1
 Memory can be ADDR2
ADDR3
conceptualized as a linear ADDR4
ADDR5
set of data locations. ADDR6
*
 Variables reference the *
*
contents of a locations ADDR11 Contents11

 Pointers have a value of the *


*
address of a given location
ADDR16 Contents16
Pointer
 Examples of pointer declarations:
FILE *fptr;
int *a;
float *b;
char *c;
 The asterisk, when used as above in the declaration, tells the
compiler that the variable is to be a pointer, and the type of
data that the pointer points to, but NOT the name of the
variable pointed to.
Use of & and *
 When is & used?

 When is * used?

 & -- "address operator" which gives or produces the


memory address of a data variable
 * -- "dereferencing operator" which provides the
contents in the memory location specified by a
pointer
Pointers and Functions
 Pointers can be used to pass addresses of variables
to called functions, thus allowing the called function
to alter the values stored there.

 We looked earlier at a swap function that did not


change the values stored in the main program
because only the values were passed to the function
swap.

 This is known as "call by value".


Pointers and Functions
 If instead of passing the values of the variables to the called
function, we pass their addresses, so that the called function
can change the values stored in the calling routine. This is
known as "call by reference" since we are referencing the
variables.

 The following shows the swap function modified from a


"call by value" to a "call by reference". Note that the values
are now actually swapped when the control is returned to
main function.
Pointers with Functions (example)
#include <stdio.h> void swap( int *a, int *b )
void swap ( int *a, int *b ) ; {
int main ( ) int temp;
{ temp= *a; *a= *b; *b = temp ;
int a = 5, b = 6; printf ("a=%d b=%d\n", *a, *b);
printf("a=%d b=%d\n",a,b) ; }
swap (&a, &b) ; Results:
printf("a=%d b=%d\n",a,b) ; a=5 b=6
return 0 ; a=6 b=5
} a=6 b=5
Arithmetic and Logical
Operations on Pointers
 A pointer may be incremented or decremented

 An integer may be added to or subtracted from a


pointer.

 Pointer variables may be subtracted from one


another.

 Pointer variables can be used in comparisons, but


usually only in a comparison to NULL.
Using the C Language Special
Keyword
size of
 This keyword can be used to determine the number
of bytes in a data type, a variable, or an array
 Example:
double array [10];
sizeof (double);/* Returns the value 8 */
sizeof (array); /* Returns the value 80 */
sizeof(array)/sizeof(double); /* Returns 10 */
E-0
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,
char b=‘B’;
float c=5.5;
clrscr();
printf(“%d is stored in %u”,a,&a);
printf(“%d is stored in %u”,b,&b);
printf(“%d is stored in %u”,c,&c);
getch();
}
E-1
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,*p;
x=10;
p=&x;
y=*p;
printf("value of x is %d \n",x);
printf("%d value is stored in %u \n",*&x,&x);
printf("%d value is stored in %u \n",*p,p);
printf("%d value is stored in %u \n",p,&p);
printf("%d value is stored in %u \n",y,&y);
*p=50;
printf("The value of x is %d",x);
getch();
}
E-2
#include<stdio.h>
#include<conio.h>
void main()
{
int a,*b,**c;
clrscr();
a=10;
b=&a;
c=&b;
printf("%u",**c);
getch();
}
E-3
#include<stdio.h>
#include<conio.h>
void main()
{
int b,i,*p,sum;
int a[5]={1,2,3,4,5};
clrscr();

sum=0;
i=0;
p=a;
while(i<5)
{
printf("a[%d] %u %u \n",i,p,*p);
sum=sum+*p;
i++;p++;
}
printf("Sum=%d",sum);
getch();
}
E-4
#include<stdio.h>
#include<conio.h>
void main()
{
int b,i,*p,sum;
int a[5]={1,2,3,4,5};
clrscr();

sum=0;
i=0;
p=a;
while(i<5)
{
printf("a[%d] %u %u \n",i,p,*p);
sum=sum+*p;
i++;p++;
}
printf("Sum=%d",sum);
getch();
}
E-5
#include<stdio.h> swap(int *x,int *y)
#include<conio.h>
{
swap(int *,int *);
void main() int z;
{ z=*x;
int a,b,c; *x=*y;
clrscr();
*y=z;
a=10;
b=20; }
printf("Before Swapping \n");
printf("a=%d \t b=%d",a,b);
swap(&a,&b);
printf("After Swapping \n");
printf("a=%d \t b=%d",a,b);
getch();
}
E-6
#include<stdio.h>
#include<conio.h>
int *large(int *,int *);
void main()
{
int a=10;
int b=20;
int *p;
clrscr();
p=large(&a,&b);
printf("%d",*p);
getch();
}
int *large(int *m,int *n)
{
if(*m>*n)
return (m);
else
return (n);
}
If there is a Will, There is a way
Practice makes perfect

Thank You

You might also like