Beruflich Dokumente
Kultur Dokumente
Pointers are often considered to be the tough most chapter in C. Many found it hard to understand it.
But I believe if carefully understand things mentioned below you will say it is the easiest chapter in C.
Best of luck....................
Definition
A pointer is a variable which contains the address of a memory location of another variable, rather than
its stored value.
type *name;
The ‘&’ is a unary operator and it returns the memory address of its operands
Ex: n=&m;
The second pointer operator ‘*’ is the complement of ‘&’. It is a unary operator that returns the value
located at the address.
Ex: if ‘n’ contains the memory address of ‘m’, then a=*n places the value of ‘m’ into ‘a’
Try this program now
#include <stdio.h>
#include <conio.h>
void main()
{
int a = 10;
int *p;
p = &a;
printf("\nAddress of a: %u", &a);
printf("\n\nAddress of a: %u", p);
printf("\n\nAddress of p: %u", &p);
printf("\n\nValue of p: %d", p);
printf("\n\nValue of a: %d", a);
printf("\n\nValue of a: %d", *(&a));
printf("\n\nValue of a: %d", *p);
getch();
}
Pointer Operations:
Addition & subtraction are the only operations which can be used with pointers. Two pointers can be
compared in a relational expression. However, this is possible only if both these variables are pointing to
variables of same type.
There is a close relationship between pointers and arrays. Consider the following:
p1=str;
Here ‘p1’ has been set to the address of the first array element in ‘str’. To access fifth element in ‘str’
you could write-- str[4] or *(p1 + 4)
Thank you..............