Pointers in C

Pointers in C Programming Language

With the aid of examples, you will learn about pointers in C in this tutorial, including what they are, how to use them, and the typical errors you may make.

Programming in C and C++ uses pointers, which are strong features. Let’s first study addresses in C programming before we learn about pointers.

Address in C

If you have a variable named var in your program, the command &var will show you where in memory it is located.

While using the scanf() function, the address has been utilized several times.

scanf(“%d”, &var);

The address of the var variable in this case stores the value that the user input. Let’s use a practical illustration.

#include <stdio.h>
int main()
{
  int var = 5;
  printf("var: %d\n", var);

  // Notice the use of & before var
  printf("address of var: %p", &var);  
  return 0;
}

Output

var: 5
address of var: 2686778

Note: When you run the aforementioned code, you probably won’t get the same address.

Pointers In C

Pointers are special variables that store addresses rather than values. They are also known as pointer variables.

Pointer Syntax

This is how pointers can be declared.

int* p;

Here, an int-type pointer named p has been declared.

Pointers may also be declared in these ways.

int *p1;
int * p2;

Let’s look at another declaration of pointers example.

int* p1, p2;

Here, a pointer named p1 and a regular variable named p2 has been declared.

Giving Pointers addresses(pointers in c)

Take this as a case study.

int* pc, c;
c = 5;
pc = &c;

The c variable in this case has the value 5. The PC pointer is also given the address of c.

Get Thing Pointed by Pointers’ Value

The * operator is used to retrieve the value of the object to which the pointers point. For instance:

int* pc, c;
c = 5;
pc = &c;
printf("%d", *pc);   // Output: 5

The PC pointer is given the address of c in this instance. We used *pc to retrieve the value kept at that address.

Remember that pc, not *pc, is a pointer in the example above. You cannot, and you should not use the syntax *pc = &c;
By the way, the dereference operator is referred to as *. (when working with pointers). It uses a pointer as input and outputs the value that the pointer contains.

Changing Value Pointed by Pointers

Changing Value Pointed by Pointers

int* pc, c;
c = 5;
pc = &c;
c = 1;
printf("%d", c);    // Output: 1
printf("%d", *pc);  // Ouptut: 1

We have given the computer pointer the address of c.

Then, we set c to have a value of 1. Since pc and the address of c is the same, *pc gives us 1.

Take yet another illustration.

int* pc, c;
c = 5;
pc = &c;
*pc = 1;
printf("%d", *pc);  // Ouptut: 1
printf("%d", c);    // Output: 1

We have given the computer pointer the address of c.

Then, we used *pc = 1; to modify *pc to 1. Since pc and c’s address match, c will have a value of 1.

Let’s look at one more instance.

int* pc, c, d;
c = 5;
d = -15;

pc = &c; printf("%d", *pc); // Output: 5
pc = &d; printf("%d", *pc); // Ouptut: -15

The pc pointer is initially given the address of c by using the expression pc = &c; Since c is 5, we get 5 from *pc.

Then, using pc = &d; the address of d is allocated to the PC pointer. D being -15, *pc returns -15.

Example: Working of Pointers(pointers in c)

Let’s take a working example.

#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 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

Explanation of the program (pointers in c)

  • Here, an int-type pointer pc and a regular variable c are both generated.
  • The initialization of pc and c leaves pointer pc pointing to either no address or a random address. Additionally, variable c has an address but is otherwise empty.
  • This gives the variable c the value 22. In other words, the variable c’s memory address contains the value 22.
  • This gives the pointer pc the address of the variable c.
  • This gives variable c the value 11
  • This updates the value to 2 at the memory address that the pointer pc was pointing at.

Common errors made when using pointers

Consider the scenario where you want pointer pc to point to the address of c. Then,

int c, *pc;

// pc is address but c is not
pc = c;  // Error

// &c is address but *pc is not
*pc = &c;  // Error

// both &c and pc are addresses
pc = &c;  // Not an error

// both c and *pc are values 
*pc = c;  // Not an error

Here’s an example of pointer syntax beginners often find confusing.

#include <stdio.h>
int main() {
   int c = 5;
   int *p = &c;

   printf("%d", *p);  // 5
   return 0; 
}

Why didn’t we get an error when using int *p = &c;?

It’s because

int *p = &c;

is equivalent to

int *p:
p = &c;

We are generating a pointer p (not *p) in both instances and assigning &c to it.

We can use the following assertion to clear up this misunderstanding:

int* p = &c;

Given that you now understand what pointers are, the following tutorial will teach you how they relate to arrays.

You may like :

Data Types In C with Example

Leave a Reply