Satish Kumar said:
Can any one give me indetail about,
Pointers are variables that can hold addresses. Normally, they get
assigned a value that is the address of another object (a normal
variable, a function or another pointer). Once that has happened
you can access what the pointer points to by dereferencing it,
which is done by putting the (unary) '*' operator in front of
the pointer variables name.
So when you have e.g.
int i = 5;
int *ip = &i; /* pointer, pointing to i */
then you can e.g. do
printf( "i is %d\n", *ip );
or
*ip = 7; /* change i indirectly via the pointer */
printf( "i is now %d\n", i )
and this will print out
i is now 7
How are pointers useful in C prgramming?
First of all, they allow you to pass addresses of variables to
functions, and by doing so it is possible to change the content
of the variable pointed to from within the function (remember:
in C variables are passed by value, so a function only receives
the value of an argument variable and can't change the variable
itself).
Second, pointers are indispensable when you need amounts of
memory you can only determine while the program is already
running. Functions like malloc() return you a pointer to a
memory buffer with a size you can tell the function. If the
function succeeds you can use the returned value to access
that memory, e.g.
int *a = malloc( 100 * sizeof *int );
if ( a != NULL ) {
a[ 17 ] = 42;
free( a );
}
But there are hundreds of other uses of pointers and listing
only the small subset which immediately comes to mind would
make this posting much too long. C without pointers would be
a rather useless language.
What do you mean by "work"?
i had gone through one book, i am a bit confused.
We all started that way (more or less). Keep reading and experi-
menting and you will understand soon enough;-) Trying to draw
some boxes representing pointers and variables and arrows de-
picting the relationship between them sometimes helps (especially
if you start playing around with pointers to pointers).
Regards, Jens