Pointers in C store the address of another variable. They allow dynamic memory allocation at runtime and can refer to variables of any data type. Pointers help save memory space and improve performance. A pointer variable contains the address of another variable. Common pointer types include null pointers, void pointers, and wild pointers. Pointers are useful for accessing memory locations and forming complex data structures like linked lists. However, pointers also present risks like memory corruption if misused.
1. Pointers In C?
POINTER is a variable that stores address of another variable. A
Pointer in C is used to allocate memory dynamically i.e. at run
time. The pointer variable might be belonging to any of the
data type such as int, float, char, double, short etc.
The purpose of pointer is to save memory space and achieve
faster execution time.
2. Pointer Syntax
data_type *var_name;
Example : int *p; char *p;
Where, * is used to denote that “p” is pointer variable and not a normal
variable.
KEY POINTS TO REMEMBER ABOUT POINTERS IN C:
1. Normal variable stores the value whereas pointer variable stores the
address of the variable.
2. The content of the C pointer always be a whole number i.e. address.
3. Always C pointer is initialized to null, i.e. int *p = null.
4. The value of null pointer is 0.
3. Let's see some valid pointer declarations
int *x; /* pointer to an integer */
int *xyz, dafar; /* xyz is a pointer to type integer and dafar is an integer variable */
double *ptr2; /* pointer to a double */
float *ptr3; /* pointer to a float */
char *ch1 ; /* pointer to a character */
float *ptr, nalayak;
/*ptr is a pointer to type float and nalayak is an ordinary float variable */
4. Assigning addresses to Pointers
Let's take an example.
Int *pc, c;
c = 5;
pc = &c;
Here, 5 is assigned to the c variable.
And, the address of c is assigned to the pc
pointer.
5
Variable c
Value
Address 2008
2008
Pointer Variable
pc
Value
Address 5060
NOTE:
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.
5. A Simple program for pointer:
#include <stdio.h>
void main()
{
int a=10; // variable declaration
int *p; // pointer variable declaration
p = &a;// store address of variable ‘a’ in pointer ‘p’
printf(“Address stored in a variable p is: %xn”, p);
// accessing the address
printf(“Value stored in a variable p is : %dn”, *p);
// accessing the value
getch();
}
Output:
Address stored in a variable p is: 60ff08
Value stored in a variable p is: 10
6. Types of a pointer
1. Null pointer
2. void pointer
3. wild pointer
7. Null pointer
We can create a null pointer by assigning null value during the pointer
declaration. This method is useful when you do not have any address assigned
to the pointer. A null pointer always contains value 0.
#include <stdio.h>
void main()
{
int *p = NULL; //null pointer
printf(“The value inside variable p is:%x”,p);
}
Output:
The value inside variable p is : 0
8. void pointer
In C programming, a void pointer is also called as a generic pointer. It does not have any
standard data type. A void pointer is created by using the keyword void. It can be used to store
an address of any variable.
#include <stdio.h>
void main()
{
void *p = NULL; //void pointer
printf("The size of pointer is:%d",sizeof(p));
getch();
}
Output:
The size of pointer is : 4 or 8 (bytes)
(depending on system you have)
9. Wild pointer
A pointer is said to be a wild pointer if it is not being initialized to anything. These types of
pointers are not efficient because they may point to some unknown memory location which
may cause problems in our program and it may lead to crashing of the program. One should
always be careful while working with wild pointers.
#include <stdio.h>
void main()
{
int *p; //wild pointer
printf("n%d",*p);
getch();
}
Output:
Output would be unexpected…………….
10. Let's take an example.
int *pc, c;
c = 5;
pc = &c;
c = 1;
printf("%d", c); // Output: 1
printf("%d", *pc); // Output: 1
Changing Value Pointed by Pointers
Let’s take an example.
int *pc, c;
c = 5;
pc = &c;
*pc = 1;
printf("%d", *pc); // Output: 1
printf("%d", c); // Output: 1
11. #include <stdio.h>
int main()
{
int* pc, c;
c = 22;
printf("Address of c: %pn", &c);
printf("Value of c: %dnn", c); // 22
pc = &c;
printf("Address of pointer pc: %pn", pc);
printf("Content of pointer pc: %dnn", *pc); // 22
c = 11;
printf("Address of pointer pc: %pn", pc);
printf("Content of pointer pc: %dnn", *pc); // 11
*pc = 2;
printf("Address of c: %pn", &c);
printf("Value of c: %dnn", c); // 2
getch();
}
Output
Address of c: 2686784
Value of c: 22
Address of pointer pc: 2686784
Content of pointer pc: 22
Address of pointer pc: 2686784
Content of pointer pc: 11
Address of c: 2686784
Value of c: 2
12. Difference between %u, %x, %X and %p
1. %u prints the value as an unsigned decimal
2. %x prints the value as a unsigned hex value - using lower case a, b,
c, e, d, f
3. %X prints the value as unsigned hex value using upper case A, B, C,
D, E, F.
4. %p is the proper format string for pointer printing.
13. Advantages of Pointers
• Pointers are useful for accessing memory locations.
• Pointers provide an efficient way for accessing the elements of an
array structure.
• Pointers are used for dynamic memory allocation as well as
deallocation.
• Pointers are used to form complex data structures such as linked list,
graph, tree, etc.
14. Disadvantages of Pointers
• Pointers are a little complex to understand.
• If an incorrect value is provided to a pointer, it may cause memory
corruption.
• Pointers are also responsible for memory leakage.
• Programmers find it very difficult to work with the pointers; therefore
it is programmer's responsibility to manipulate a pointer carefully.
15. Pointers with Array
we access the array elements using its index, but this method can be
eliminated by using pointers. Pointers make it easy to access each array
element.
#include <stdio.h>
void main()
{
int a[5]={1,2,3,4,5}; //array initialization
int *p; //pointer declaration
/*the ptr points to the first element of the array*/
p=a; /*We can also type simply ptr==&a[0] */
printf("Printing the array elements using pointern");
for(int i=0;i<5;i++) //loop for traversing array elements
{
printf("n%x",*p); //printing array elements
p++;
//incrementing to the next element, you can also write p=p+1
}
getch();
} The Output is:
1
2
3
4
5
16. Let’s take another example of “Pointers with Array”.
#include <stdio.h>
void main() {
int x[5] = {1, 2, 3, 4, 5};
int* ptr;
// ptr is assigned the address of the third element
ptr = &x[2];
printf("*ptr = %d n", *ptr); // 3
printf("*(ptr+1) = %d n", *(ptr+1)); // 4
printf("*(ptr-1) = %d", *(ptr-1)); // 2
getch();
}
When you run the program,
the output will be:
*ptr = 3
*(ptr+1) = 4
*(ptr-1) = 2
17. Relationship Between Arrays and Pointers
#include <stdio.h>
int main() {
int x[4];
int i;
for(i = 0; i < 4; ++i) {
printf("&x[%d] = %pn", i, &x[i]);
}
printf("Address of array x: %p", x);
getch();
}
output:
&x[0] = 1450734448
&x[1] = 1450734452
&x[2] = 1450734456
&x[3] = 1450734460
Address of array x: 1450734448
18. From the above example, it is clear that &x[0] is equivalent to x. And, x[0] is
equivalent to *x.
Similarly,
&x[1] is equivalent to x+1 and x[1] is equivalent to *(x+1).
&x[2] is equivalent to x+2 and x[2] is equivalent to *(x+2).
...
Basically, &x[i] is equivalent to x+i and x[i] is equivalent to *(x+i).
19. Pointer and Strings
#include <stdio.h>
#include <string.h>
void main()
{
char str[]="Hello Dafars!";
char *p;
int i;
p=str;
printf("First character is:%cn",*p);
p =p+1;
printf("Next character is:%cn",*p);
printf("Printing all the characters in a
stringn");
p=str; //reset the pointer
for(i=0; i< strlen(str); i++)
{
printf("%cn",*p);
p++;
}
getch();
}
20. Functions with Array Parameters
In C, we cannot pass an array by value to a function. Whereas, an array name is a pointer
(address), so we just pass an array name to a function which means to pass a pointer to the
array.
int add_array (int *p, int size);
int main()
{
int arr[5] = {100, 220, 37, 16, 98};
printf(“Sum is %dn", add_array(arr, 5));
getch();
}
int add_array (int *p, int size)
{
int total = 0;
int k;
for (k = 0; k < size; k++)
{
total += p[k];
/* it is equivalent to total +=*p ;p++; */
}
return (total);
}
Output:
Sum is 471
21. Passing Pointers to Functions
#include <stdio.h>
void addOne(int *ptr);
void main()
{
int* p, i = 10;d
p = &i;
addOne(p);
printf("%d", *p); // 11
getch();
}
void addOne(int *ptr)
{
(*ptr)++; // adding 1 to *ptr
}