Pointer representation to a two dimensional array (matrix):
When a matrix is declared then the compiler allocates the memory allocation of single dimensional arrays (rows) in contiguous memory allocations.
Address of each row is stored in a pointer array.
The address of pointer array is again stored in a pointer-to-pointer with the name of matrix.
Now the pointer-to-pointer is capable of accessing every element of matrix using pointer arithmetics.
Example:
int a[3][5];
Note:
*(a+i)+j can be used instead of &a[i][j] to read the data from keyboard and store into array.
*(*(a+i)+j) can be used instead of a[i][j] to access the elements of array.
Example:
#include<stdio.h>
void main()
{
int a[3][4]={{12,34,32,11},{10,19,28,27},{72,81,92,93}};
int i,j;
clrscr();
printf("Elements of matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
printf("%5d",*(*(a+i)+j));
printf("\n");
}
getch();
}
Output:
12 34 32 11
10 19 28 27
72 81 92 93
Specification1:
Accept a nxm matrix and print the matrix as accepted using pointer arithmetics.
Program:
#include<stdio.h>
void main()
{
int a[50][50],n,m,i,j;
clrscr();
printf("Enter the class of matrix:\n");
scanf("%d%d",&n,&m);
printf("Enter %dx%d matrix:\n",n,m);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",*(a+i)+j);
printf("The given matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%5d",*(*(a+i)+j));
printf("\n");
}
getch();
}
Execution:
Enter the class of matrix:
3 4
Enter 3x4 matrix:
12 34 65 78
22 12 34 55
64 56 23 32
The given matrix:
12 34 65 78
22 12 34 55
64 56 23 32
Sending the matrix as an argument (Address of matrix):
While sending a matrix as an argument, we specify the name of matrix as an actual argument which is the pointer array to matrix.
Here the formal argument must be a pointer to array.
Now the formal argument can refer every element of array using pointer arithmetics.
Example:
void display(int*[50],int,int);
void main()
{
int a[50][50],n,m,i,j;
clrscr();
printf("Enter the class of matrix:\n");
scanf("%d%d",&n,&m);
printf("Enter %dx%d matrix:\n",n,m);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",*(a+i)+j);
display(a,n,m);
getch();
}
void display(int (*P)[50],int n,int m)
{
int i,j;
printf("The given matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%5d",*(*(p+i)+j));
printf("\n");
}
}
Returning a pointer:
By returning the address of either a variable or an array to the calling function, we can allow the calling function to access the contents of called by function.
The scope (life) of non-static variables/arrays is limited to the function, the memory allocation of which is de-allocates on completion of function execution.
Only the memory allocation of static variables/arrays retains even after the completion of function execution.
Hence the address of either static variables/arrays can be returned to the calling function to the called by function.
Returning the address of a variable:
Example1:
#include<stdio.h>
int* display();
void main()
{
int *p;
clrscr();
p=display();
printf("x=%d",*p);
getch();
}
int* display()
{
static int x=10;
return &x;
}
Output:
x=10
Example explained:
"x" is an integer variable belongs to function display().
On calling the function display() returns the address of "x" which is assigned to integer pointer "p"
Now "p" is the pointer to "x"
*p (value at "p") is 10.
Make sure that "x" must be static to retain its memory even after returning to main()
Returning the address of an array/Returning an array from the function:
#include<stdio.h>
int* vector();
void main()
{
int *p;
int i;
clrscr();
p=vector();
for(i=0;i<5;i++)
printf("%5d",*(p+i));
getch();
}
int* vector()
{
static int x[]={10,25,33,34,55};
return x;
}
Output:
10 25 33 34 55
Example explained:
Here "x" is the pointer to array (int type).
The address of array (x) is returned and assigned to int type of pointer "p".
Now "p" is capable to access the elements of array using pointer arithmetics.
Not that array "x" is of static type hence its memory retains even after the completion of vector() execution.
Pointer to a function:
Like a variable and an array even a function is stored in the memory some where.
The address of which is stored in a pointer with the name of function.
By using indirect operator * (value at) we can call the function through pointer.
Example1:
#include<stdio.h>
void display()
{
printf("Hello world");
}
void main()
{
clrscr();
(*display)(); /* value at display*/
getch();
}
Output:
Hello world
Example explained:
Here "display" is the pointer to the function display().
By using * (value at) we can call the function through pointer.
Example2:
#include<stdio.h>
int getsum(int x,int y)
{
return x+y;
}
void main()
{
int a,b,c;
clrscr();
printf("Enter two numbers:\n");
scanf("%d%d",&a,&b);
c=(*getsum)(a,b);
printf("Sum=%d",c);
getch();
}
Execution:
Enter two numbers:
12
34
Sum=46
Declaring a pointer to function:
Syntax:
<return type> (*<name of pointer>)(<list of arguments types>)
Example3:
#include<stdio.h>
void display()
{
printf("Hello world");
}
void main()
{
void (*p)();
clrscr();
p=&display;
(*p)();
getch();
}
Output:
Hello world
Example explained:
"p" is a function type of pointer which returns void and accepts no arguments.
The address of function display() is stored in a pointer "p"
Now pointer can refer the function using indirect operator value at (*)
Sending the address of a function as argument:
Example4:
#include<stdio.h>
void saynice()
{
printf("Hello Nice");
}
void sayworld()
{
printf("\nHello World");
}
void display(void (*p)(),void (*q)())
{
(*p)();
(*q)();
}
void main()
{
clrscr();
display(&saynice,&sayworld);
getch();
}
Output:
Hello Nice
Hello World
Example explained:
Sending the addresses of saynice() and sayworld() to display()
The display() function calls both the functions using * (value at )
Example5:
#include<stdio.h>
int getsum(int x,int y)
{
return x+y;
}
int getsub(int x,int y)
{
return x-y;
}
void manip(int (*p)(int,int),int (*q)(int,int))
{
int a,b,c,d;
printf("Enter two numbers:\n");
scanf("%d%d",&a,&b);
c=(*p)(a,b);
d=(*q)(a,b);
printf("Sum %d",c);
printf("\nSubtaction %d",d);
}
void main()
{
clrscr();
manip(&getsum,&getsub);
getch();
}
Output:
Enter two numbers:
12
6
Sum 18
Subtraction 6
Example explained:
Addresses of getsum() and getsub() are send to manip().
Both "p", "q" are pointers to getsum() and getsub().
Using * (value at) "p" and "q" called the functions.
Pointer array to functions:
Array of pointers can be defined to store the addresses of different functions.
These functions can be called in a sequence.
Example:
#include<stdio.h>
void one()
{
printf("Keep it ");
}
void two()
{
printf("small ");
}
void three()
{
printf("and simple");
}
void main()
{
void (*p[3])();
int i;
clrscr();
p[0]=&one;
p[1]=&two;
p[2]=&three;
for(i=0;i<3;i++)
(*(p[i]))();
getch();
}
Output:
Keep it small and simple
Example explained:
"p" is a pointer array refers three functions.
By using indirect operator * (value at) we can access functions through pointer array.
0 comments:
Post a Comment