Pointers
A pointer is a variable that stores the address of another variable. Normally a variable stores a value, but a pointer stores the address of another variable.
Each variable occupies some memory in the computer, which is given by its address. To find the address of the variable, use the "&" operator.
e.g.
int a = 10;
printf("The value is %d ", a);
printf("The address is %u", &a);
The address of a variable can then be stored in a pointer variable. To declare a pointer variable to hold the address of an integer,
int *p;
p is a pointer to an integer.
To assign a value to this pointer variable
p = &a;
Now p contains the address of the integer variable a.
Showing posts with label pointers in C. Show all posts
Showing posts with label pointers in C. Show all posts
Sunday, 31 March 2013
Wednesday, 13 March 2013
pointers in C
A pointer is a variable which contains the address of another variable. If one variable contains address of another variable, first variable is said to point to the second.
Pointer provides a way of accessing variable without referring to the variable directly. It provides a symbolic way to using addresses.
Declaration of pointer variable:
pointers are declared as follows:
datatype *variable_name;
eg. int *p;
In above example *p is a pointer variable of integer type.
There are two special operators used with pointer:
1) * - It returns the value contained in memory location.
2) & - Is a unary operator and returns a memory address of an operand.
eg. p=&a;
In above example, p is a pointer variable which stores address of a.
Pointer notation:
Consider the declaration
int i = 3;
This declaration tells the C Compiler to
a) Reserve space in memory to hold the integer value.
b) Associate the name i with the memory location.
c) Store the value 3 at this location.
We may represent this location in memory by following memory map.
i -> location name
3 -> value at location
Uses of pointer:
1) To return more than one value from the function.
2) To pass arrays and strings more conventionally from one function to another.
3) To manipulate arrays more easily.
4) To allocate memory and access it.
5) For faster execution of program.
6) To create complex data structure such as linked list.
Pointer provides a way of accessing variable without referring to the variable directly. It provides a symbolic way to using addresses.
Declaration of pointer variable:
pointers are declared as follows:
datatype *variable_name;
eg. int *p;
In above example *p is a pointer variable of integer type.
There are two special operators used with pointer:
1) * - It returns the value contained in memory location.
2) & - Is a unary operator and returns a memory address of an operand.
eg. p=&a;
In above example, p is a pointer variable which stores address of a.
Pointer notation:
Consider the declaration
int i = 3;
This declaration tells the C Compiler to
a) Reserve space in memory to hold the integer value.
b) Associate the name i with the memory location.
c) Store the value 3 at this location.
We may represent this location in memory by following memory map.
i -> location name
3 -> value at location
Uses of pointer:
1) To return more than one value from the function.
2) To pass arrays and strings more conventionally from one function to another.
3) To manipulate arrays more easily.
4) To allocate memory and access it.
5) For faster execution of program.
6) To create complex data structure such as linked list.
Subscribe to:
Posts (Atom)