Professional Documents
Culture Documents
1.
2.
3.
4.
5.
6.
They increase the execution speed and thus reduce the program execution
time.
Understanding pointers
Memory organisation
Memory cell
Address
0
1
2
3
:
:
:
65,535
int n;
Memory cell
n= 30;
Address
:
:
Variable
30
250
251
Value
30
:
:
Address
251
P= 250
:
65,535
Syntax:
data_type *pt_name;
2.
3.
Eg:
int *p;
float *x;
All the pointer variables should be initialized before they are used in the
program.
int n;
int *p;
declaration
n=30;
p= &n;
initialization
int * p= &n;
float a, b;
int x, *p;
p= &a;
b= *p;
WRONG
int x, *p = x;
int *p = &x, x;
int *p=NULL;
int *p=0;
int *p = 5360;
nn==256
5
Pointer flexibility
int x,y,z,*p;
............
p=&x;
............
p=&y;
............
p=&z;
............
Pointer flexibility
int x;
int *p=&x;
int *q=&x;
int *r=&x;
1000
Elements
Values
Address
X[0]
X[1]
X[2]
X[3]
X[4]
1000
1002
p=&x[0]
(= 1000)
p+1= &x[1]
(= 1002)
p+2= &x[2]
(= 1004)
p+3= &x[3]
(= 1006)
p+4= &x[4]
(= 1008)
1004
1006
1008
int x[5];
int *p;
p=&x;
void main()
for(i=0;i<n;i++)
{
int a[30];
printf("%d\n",*ptr);
int i,n,*ptr;
ptr++;
ptr=a;
getch();
}
s++;
len++
}
printf("The length is %d",len);
getch();
}
ptr++;
}
len=ptr-s;
printf("The length is %d",len);
getch();
}
Function Arguments:
While calling a function, there are two ways that arguments can be passed to a
function:
Pass
by value
Pass
by reference
In this case, changes made to the parameter inside the function have no
effect on the argument.
It allows transfer of only zero or one value only through the return
mechanism.
#include <stdio.h>
void change(int);
void main()
{
int n;
clrscr();
printf("Enter the values of n : ");
scanf("%d",&n);
printf("\nThe value of n before function call is %d",n);
change(n);
printf("\nThe value of n after function call is %d",n);
getch();
}
void change(int n)
{
n=n+30;
printf("\nThe value of n within function is %d",n);
}
{
// local variable declaration:
int a,b;
temp = a;
clrscr();
a= b;
b = temp;
scanf("%d %d",&a,&b);
printf("Before swap, value of a : %d", a);
printf("\nBefore swap, value of b : %d",b);
swap(a, b);
printf("\nAfter swap, value of a :%d",a);
printf("\nAfter swap, value of b : %d", b);
getch();
return 0;
}
Inside the function, the address is used to access the actual argument used in
the call.
This means that changes made to the parameter affect the passed argument.
When pointers are used as arguments asterisk (*) must precede the formal
pointer argument.
#include <stdio.h>
void change(int *n);
void main()
*n=*n+30;
int n;
clrscr();
printf("Enter the values of n : ");
scanf("%d",&n);
printf("\nThe value of n before function call is %d",n);
change(&n);
printf("\nThe value of n after function call is %d",n);
getch();
{
// local variable declaration:
int a,b;
temp = *a;
clrscr();
printf("Enter two values of a and b : ");
*a= *b;
scanf("%d %d",&a,&b);
*b = temp;
#include <stdio.h>
void concatenate_string(char*, char*);
int main()
{
//char original[100], add[100];
char *str1,*str2; clrscr();
printf("Enter source string\n");
gets(str1);
Pointer Arithmetic
Incremented
ii)
Decremented
iii)
iv)
int a=50;
50
2004
2006
2004
5000
int *p;
p=&a;
p++;
p--;
p-2;
p+5;
2004
Array of pointers
char name[5][30];
char *name[5];