Pointers contain memory addresses, NOT data values.
—
effectively represent sophisticated data
structures
—
change values of actual arguments
passed to functions (“call-by-reference”)
—
work with memory which has been dynamically
allocated
—
more concisely and efficiently
deal with arrays
You can find out the memory address of a variable by simply
using the address operator &.
Here is an example of its use:
&v
The above expression
should be read as “address of v”, and it returns the memory address of the
variable v.
dataType *pointername;
Ex: int *p;
Declares p as a pointer
variable that points to integer data type.
float *x;
char *y;
main() {
int i=3;
int *j; /*
declaration */
j = &i; /*
initialization */
printf(“ Address of
i = %u”, &i);
printf(“ Contents of j = %u”, j);
printf(“ Value of i = %d”, i);
printf(“Value of i accessed through pointer j = %d”, *j);
}
Pointers are flexible:
we can make same pointer to point to different
data variables in different statements.
int x, y, z, *p;
P = &x;
---------
P = &y;
---------
P = &z;
----------
SWAP - Using Pointers
main()
{
int i=3, t, j=76;
int *p, *q;
p=&i; q=&j;
t=*p;
*p=*q;
*q=t;
printf("After swap, i=%d j=%d", i, j);
}
main() {
int i=3, j=76;
swap(i, j);
printf("After swap, i=%d j=%d\n",i,j);
}
void swap(int p, int
q) {
int t;
t=p;
p=q;
q=t;
return p;
}
Note: Two values are
to be returned -à NOT POSSIBLE
SWAP - Using Pointers & Functions
main( ) {
int i=3, j=76;
swap(&i, &j);
printf("After swap, i=%d j=%d\n", i, j);
}
void swap( int *p, int *q) {
int t;
t = *p;
*p = *q;
*q = t;
}
int i=3,j=76; // GLOBAL
VARIABLES
main( ) {
swap( );
printf("After swap, i=%d j=%d\n",i,j);
}
void swap() {
int t;
t=i;
i=j;
j=t;
}
int arr[5];
int *ptr;
ptr = &arr[0]; à Now ptr is a pointer to
array arr.
Qn: Program to find
the sum of array using pointers?
Pointers & Arrays
main() {
int *p, sum, i=0,arr[5];
arr[] = {1, 2, 3, 4, 5};
p = arr; OR p = &arr[0];
while( i < 5) {
sum = sum + *p;
p++; i++; }
printf(“ Sum = %d”, sum);
}
Consider the following:
int myarray[] = { 1,
23, 17, 4, -5, 100 };
Here we
have an array containing 6 integers.
We refer to
each of these integers by means of a subscript to myarray, i.e. using
myarray[0] through myarray[5].
But, we could alternatively access them via a pointer
as follows:
int *ptr;
ptr =
&myarray[0]; /* point our pointer at the first integer in our array */
What happens when we write:
ptr
+ 1;
Because the compiler "knows" this is a pointer
(i.e. its value is an address) and that it points to an integer (its current
address, 100, is the address of an integer), it adds 2 to ptr instead of
1, so the pointer "points to" the next integer, at memory location
102.
int myarray[] = {1, 23, 17, 4, -5, 100};
int *ptr;
int
main(void) {
int i;
ptr =
&myarray[0]; /* point our pointer to the first element of the array */
for (i = 0;
i < 6; i++)
{
printf(“ myarray[%d]
= %d ", i , myarray[i]); /*line A
*/
printf(“ ptr
+ %d = %d ", i , *(ptr + i)); /*line B */
} }
—
lines A and B and that the
program prints out the same values in either case.
Qn: WAP to find the sum of 10 numbers in an array using
pointers?
Pointers, Arrays & Functions
main() {
int
abc[10],s;
printf(“eneter
the numbers”);
for(i=0;i<10;i++)
scanf(“%d” ,&abc[i]);
s = sumarray(abc);
printf(“Sum
of Array elements is %d”, s);
}
int sumarray(int *p) {
for(i=0; i<10;
i++) {
t = t +
*p;
p++; }
return t; }
Function Call :
Types
The technique used to pass DATA from one function to another
is known as parameter passing.
Two types:
Ø
CALL BY VALUE or Pass by Value
Ø
CALL BY REFERENCE or Pass by
Reference
This method is used to return multiple values by the called
function.
main() {
int i,
abc[10], lar;
printf(“
Enter the numbers”):
for(i=0;i<n;
i++)
scanf(“%d”,
abc[i]);
large(
&abc[0], &lar, n );
printf(“Largest
is %d”, lar);
}
large(int
*ptr, int *big, int m) {
*big = *ptr;
for(i=0,
i<m; i++)
{
if(
*big < *ptr)
{ *b = *ptr;
}
ptr++;
}
Qn: WAP to copy a string from s1 to s2 using pointers?
main() {
char s2[10], s1[] = “arun”;
stringcopy(s2,s1);
printf(“Source string is : %s”,s1);
printf(“Target string is : %s”,s2);
}
stringcopy(char *x2, char *x1) {
while(*x1 != ‘\0’) {
*x2 = *x1;
X1++; x2++;
}
*x2 = ‘\0’; }
Qn: Sorting using Pointers?
main(){
int i ,n, j, a[20],k;
printf(“ Enter the numbers”);
scanf(“%d”, &n);
printf(“ Enter the numbers”);
for(i=0;i<n;
i++)
scanf(“%d”, a+i);
for( i=0;
i<n; i++)
for( j=i+1; j<n;
j++) {
if(*(a+i)>*(a+j){
t
= *(a+i);
*(a+i)
= *(a+j);
*(a+j)
= t; }
printf ( “The sorted Array is”);
for(i=0;i<n; i++)
printf(“%d”, *(a+i) ); }
Pointers and 2D Arrays
int AB[3][3];
int *td;
td = AB;
Now, each element may be accessed by referring to as à
*(*(td +
i)+ j )
Qn: Sum elements in a 3X3 matrix?
main() {
int
AB[3][3]={{1,4 ,4}, {5,5,5}, {2,2,2}};
int *td;
td = AB;
for (
i=0; i<3; i++)
for ( j=0; j<3; j++)
for ( j=0; j<3; j++)
s = s + *(*(td+i)+j) ;
printf(“Sum of elements is %d”, s);
}
No comments:
Post a Comment