What is a pointer

Pointers are powerful features of C and C++ programming. Before we learn pointers, let's learn about addresses in C programming.

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.
  1. scanf("%d", &var);
Here, the value entered by the user is stored in the address of var variable. Let's take a working example.
  1. #include <stdio.h>
  2. int main()
  3. {
  4. int var = 5;
  5. printf("var: %d\n", var);
  6. // Notice the use of & before var
  7. printf("address of var: %p", &var);
  8. return 0;
  9. }
Output
var: 5 
address of var: 2686778
Note: You will probably get a different address when you run the above code.

C Pointers

Pointers (pointer variables) are special variables that are used to store addresses rather than values.

Pointer Syntax

Here is how we can declare pointers.
  1. int* p;
Here, we have declared a pointer p of int type.
You can also declare pointers in these ways.
  1. int *p1;
  2. int * p2;

Let's take another example of declaring pointers.
  1. int* p1, p2;
Here, we have declared a pointer p1 and a normal variable p2.

Assigning addresses to Pointers

Let's take an example.
  1. int* pc, c;
  2. c = 5;
  3. pc = &c;
Here, 5 is assigned to the c variable. And, the address of c is assigned to the pc pointer.

Get Value of Thing Pointed by Pointers

To get the value of the thing pointed by the pointers, we use the * operator. For example:
  1. int* pc, c;
  2. c = 5;
  3. pc = &c;
  4. printf("%d", *pc); // Output: 5
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.

Changing Value Pointed by Pointers

Let's take an example.
  1. int* pc, c;
  2. c = 5;
  3. pc = &c;
  4. c = 1;
  5. printf("%d", c); // Output: 1
  6. printf("%d", *pc); // Ouptut: 1
We have assigned the address of c to the pc pointer.
Then, we changed the value of c to 1. Since pc and the address of c is the same, *pc gives us 1.
Let's take another example.
  1. int* pc, c;
  2. c = 5;
  3. pc = &c;
  4. *pc = 1;
  5. printf("%d", *pc); // Ouptut: 1
  6. printf("%d", c); // Output: 1
We have assigned the address of c to the pc pointer.
Then, we changed *pc to 1 using *pc = 1;. Since pc and the address of c is the same, c will be equal to 1.
Let's take one more example.
  1. int* pc, c, d;
  2. c = 5;
  3. d = -15;
  4. pc = &c; printf("%d", *pc); // Output: 5
  5. pc = &d; printf("%d", *pc); // Ouptut: -15
Initially, the address of c is assigned to the pc pointer using pc = &c;. Since c is 5, *pc gives us 5.
Then, the address of d is assigned to the pc pointer using pc = &d;. Since d is -15, *pc gives us -15.

Example: Working of Pointers

Let's take a working example.
  1. #include <stdio.h>
  2. int main()
  3. {
  4. int* pc, c;
  5. c = 22;
  6. printf("Address of c: %p\n", &c);
  7. printf("Value of c: %d\n\n", c); // 22
  8. pc = &c;
  9. printf("Address of pointer pc: %p\n", pc);
  10. printf("Content of pointer pc: %d\n\n", *pc); // 22
  11. c = 11;
  12. printf("Address of pointer pc: %p\n", pc);
  13. printf("Content of pointer pc: %d\n\n", *pc); // 11
  14. *pc = 2;
  15. printf("Address of c: %p\n", &c);
  16. printf("Value of c: %d\n\n", c); // 2
  17. return 0;
  18. }
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


Post a Comment

0 Comments