Loading...
C Pointers

C Pointers

In this tutorial, we will learn about memory addresses, pointers and its working in C programming with the help of examples.


Memory Address

A Memory address is a location of the variable where data is stored.

Memory address is used to to point toward a variable as we use in scan() function with Ampersand (&) .

Each variable in a C program has different memory address.

Acessing address in the scanf() function.

scanf("%d", &variable_name);

Here, we have used & in scanf() function to point toward the memory address of the variable to store value enter by user in it.


Example : Getting memory address of variable

#include <stdio.h>
int main()
{
  int age = 18;
  printf("value of age : %d\n", var);
  // Using & before age to get memory address
  printf"address of variable age : %p", &age);
  return 0;
}

Output

value of age : 18
address of var: 2686778

In the above example, we have used & in printf statement to get the address of the variable age .


Pointers

A Pointer is a special variable which is used to store the address of another variable.

It is used to save memory space and achieve faster execution time.

Note : The data type of the pointer must be the same as the variable whose address is stored in it.

Syntax :

Data_type  *pointer_name;

Here, we have declared a pointer p of int type.


Assigning memory addresses to Pointers

#include <stdio.h>
void main() {
  int* a_address, a;
  a = 10;
  a_address = &a;
  printf("%d", a_address); //Printing address of variable a using a_address
}

Output :

1554359196

Here, we have declared two variables in which *a_address is the pointer used to store address and a is int type variable initialized with value 10. Then we have assign the address of a to the pointer *a_address using & .


Get Value of Variable using Pointer

To get value of Variable using Pointer we have to use dereference operator * or asterisk .
It is used to give or point toward the value stored at that address.

Example :

#include <stdio.h>
void main() {
  int* a_address, a;
  a = 10;
  a_address = &a;
  printf("%d", *a_address); //Using pointer to get value of variable
}

Output :

10

In the above example, we have used to pointer *a_address to point toward the value stored at that address.

Note: In the above example, a_address is a pointer, not *a_address.


Changing Value of Variable using Pointers

We can also change the value of the variable using pointer by pointing toward memory address with dereferece operator * .

Example :

#include <stdio.h>
void main() {
  int* a_address, a;
  a = 10;
  a_address = &a;
  *a_address = 5;
  printf("%d \n", *a_address);
  printf("%d", a);
}

Output :

5
5

In the above example, we have declared a_address pointer and a variable of int type initialized with value 10. Then, address of a is initialized to a_address pointer.

We have replace the value store at pointer *a_address with 50 .


Wrong ways of assigning memory address to pointers

// Declaring pointer a_address and a integer variable
int *a_address, a;
a_address = a; // Error
*a_address = &a; // Error
*a_address = a; // Error

Condition where we not get error on wrong declaration ?

#include <stdio.h>
int main() {
  int a = 10;
  int *a_address = &a;
  printf("%d", *a_address); // 10
  return 0;
}

Reason for not gettting an error when using int *a_address = &a;?

Because, we have declare and initialize the pointer in same line i.e.

int *a_address, a;
a_address = &a;

is same as

int *a_address = &a;

Next Tutorial

We hope that this tutorial helped you develop better understanding of the concept of Pointers in C.

Keep Learning : )

In the next tutorial, you'll learn about C Pointers & Arrays.

- Related Topics