It is a rare case indeed when we care what the specific address itself is, but pointers are a quite common way to get at the contents of something. The unary operator `&' is used to produce the address of an object, if it has one.
A pointer is a variable which contains the address in memory of another variable. We can have a pointer to any variable type.
The unary or monadic operator & gives the ``address of a variable''.
The indirection or dereference operator * gives the ``contents of an object pointed to by a pointer''.
To declare a pointer to a variable do:
int *pointer;
The following example illustrate the declataion of pointer variable
int *ptr;
float *string;
Address operator:
Once we declare a pointer variable then 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 example:
ptr=#
The above code tells that the address where num is stores into the variable ptr.
The variable ptr has the value 21260,if num is stored in memory 21260 address then
The following program illustrate the pointer declaration
/* A program to illustrate pointer declaration*/
main()
{
int *ptr;
int sum;
sum=45;
ptr=&sum
printf ("\n Sum is %d\n", sum);
printf ("\n The sum pointer is %d", ptr);
}
Pointer expressions & pointer arithmetic:
In expressions,like other variables pointer variables can be used.
For example if p1 and p2 are properly initialized and declared pointers, then the following statements are valid.
The following program illustrate the pointer expression and pointer arithmetic
/*Program to illustrate the pointer expression and pointer arithmetic*/
#include< stdio.h >
main()
{
int ptr1,ptr2;
int a,b,x,y,z;
a=30;b=6;
ptr1=&a;
ptr2=&b;
x=*ptr1+ *ptr2 6;
y=6*- *ptr1/ *ptr2 +30;
printf("\nAddress of a +%u",ptr1);
printf("\nAddress of b %u",ptr2);
printf("\na=%d, b=%d",a,b);
printf("\nx=%d,y=%d",x,y);
ptr1=ptr1 + 70;
ptr2= ptr2;
printf("\na=%d, b=%d,"a,b);
}
Pointers and function:
In a function declaration,the pointer are very much used .
Sometimes,only with a pointer a complex function can be easily represented and success.
In a function definition,the usage of the pointers may be classified into two groups.
Call by reference
Call by value.
Call by value:
We have seen that there will be a link established between
the formal and actual parameters when a function is invoked.
As soon as temporary storage is created where the value of actual parameters is stored.
The formal parameters picks up its value from storage area the mechanism of
data transfer between formal and actual parameters allows the actual parameters mechanism of
data transfer is referred as call by value. The corresponding formal
parameter always represents a local variable in the called function.
The current value of the corresponding actual parameter becomes the initial value of formal parameter.
In the body of the actual parameter,the value of formal parameter may be changed.
In the body of the subprogram,the value of formal parameter may be changed by assignment or input statements.
This will not change the value of the actual parameters.
/* Include< stdio.h >
void main()
{
int x,y;
x=20;
y=30;
printf("\n Value of a and b before function call =%d %d",a,b);
fncn(x,y);
printf("\n Value of a and b after function call =%d %d",a,b);
}
fncn(p,q)
int p,q;
{
p=p+p;
q=q+q;
}
Call by Reference:
The address should be pointers,when we pass address to a function the parameters receiving .
By using pointers,the process of calling a function to pass the address of the variable is known as call by reference.
The function which is called by reference can change the value of the variable used in the call.
/* example of call by reference*?
/* Include< stdio.h >
void main()
{
int x,y;
x=20;
y=30;
printf("\n Value of a and b before function call =%d %d",a,b);
fncn(&x,&y);
printf("\n Value of a and b after function call =%d %d",a,b);
}
fncn(p,q)
int p,q;
{
*p=*p+*p;
*q=*q+*q;
}
Pointer to arrays:
an array is actually very much similar like pointer.
We can declare as int *a is an address,because a[0] the arrays first element as a[0] and *a is also an address
the form of declaration is also equivalent.
The difference is pointer can appear on the left of the assignment operator and it is a is a variable that is lvalue.
The array name cannot appear as the left side of assignment operator and is constant.
/* A program to display the contents of array using pointer*/
main()
{
int a[100];
int i,j,n;
printf("\nEnter the elements of the array\n");
scanf(%d,&n);
printf("Enter the array elements");
for(I=0;I< n;I++)
scanf(%d,&a[I]);
printf("Array element are");
for(ptr=a,ptr< (a+n);ptr++)
printf("Value of a[%d]=%d stored at address %u",j+=,*ptr,ptr);
}
Pointers and structures
We know the name of an array stands for address of its zeroth element
the same concept applies for names of arrays of structures.
Suppose item is an array variable of the struct type.
Consider the following declaration:
struct products
{
char name[30];
int manufac;
float net;
item[2],*ptr;
this statement ptr as a pointer data objects of type struct products and declares item as array of two elements, each type struct products.
Pointers on pointer
A pointer contains garbage value until it is initialized.
Since compilers cannot detect the errors may not be known until we execute the program remember
that even if we are able to locate a wrong result,uninitialized or wrongly initialized pointers
it may not provide any evidence for us to suspect problems in pointers.
For example the expressions such as
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords:
Pointers in C,
double pointers in c,
pointers in c language,
pointer in c,
pointers in c++,
function pointers in c,
function pointer in c,
using pointers in c,
data types in c,
array of pointers in c,
pointers in c#,
pointer to pointer in c,
pointer to a pointer in c,
pointers to functions in c,
file pointer in c,
file pointers in c