C Pointers 12
C Pointers 12
Features of Pointers:
1. Pointers save memory space.
2. Execution time with pointers is faster because data are manipulated with the
address, that is, direct access to memory location.
3. Memory is accessed efficiently with the pointers. The pointer assigns and
releases the memory as well. Hence it can be said the Memory of pointers is
dynamically allocated.
4. Pointers are used with data structures. They are useful for representing two-
dimensional and multi-dimensional arrays.
5. An array, of any type can be accessed with the help of pointers, without
considering its subscript range.
6. Pointers are used for file handling.
Uses of pointers:
1. To pass arguments by reference
2. For accessing array elements
3. To return multiple values
4. Dynamic memory allocation
5. To implement data structures
6. To do system level programming where memory addresses are useful
Address in C
If you have a variable var in your program, &var will give you its address in the
memory.
We have used address numerous times while using the scanf() function.
scanf("%d", &var);
Here, the value entered by the user is stored in the address of var variable.
Example.
#include <stdio.h>
int main()
{
int var = 5;
printf("var: %d\n", var);
Output
var: 5
address of var: 2686778
Note: You will probably get a different address when you run the above code.
Pointer Syntax
Here is how we can declare pointers.
int* p;
int *p1;
int * p2;
int* pc, c;
c = 5;
pc = &c;
Here, the address of c is assigned to the pc pointer. To get the value stored in that
address, we used *pc.
Note: In the above example, pc is a pointer, not *pc. You cannot and should not do
something like *pc = &c;
By the way, * is called the dereference operator (when working with pointers). It
operates on a pointer and gives the value stored in that pointer.
int* pc, c;
c = 5;
pc = &c;
c = 1;
printf("%d", c); // Output: 1
printf("%d", *pc); // Ouptut: 1
int* pc, c;
c = 5;
pc = &c;
*pc = 1;
printf("%d", *pc); // Ouptut: 1
printf("%d", c); // Output: 1
int* pc, c, d;
c = 5;
d = -15;
#include <stdio.h>
int main()
{
int* pc, c;
c = 22;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 22
pc = &c;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 22
c = 11;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 11
*pc = 2;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 2
return 0;
}
Output
Address of c: 2686784
Value of c: 22
Address of c: 2686784
Value of c: 2
Explanation of the program
1. int* pc, c;
2. c = 22;
This assigns 22 to the variable c. That is, 22 is stored in the memory location of
variable c.
3. pc = &c;
4. c = 11;
This change the value at the memory location pointed by the pointer pc to 2.
#include <stdio.h>
int main() {
int c = 5;
int *p = &c;
printf("%d", *p); // 5
return 0;
}
int *p = &c;-
is equivalent to
int *p:
p = &c;
In both cases, we are creating a pointer p (not *p) and assigning &c to it.
To avoid this confusion, we can use the statement like this:
int* p = &c;
=
Double Pointer (Pointer to Pointer) in C
Last Updated: 05-12-2019
The above diagram shows the memory representation of a pointer to pointer. The first pointer ptr1
stores the address of the variable and the second pointer ptr2 stores the address of the first pointer.