C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Pointers

C Programming

In this tutorial we will learn about Pointers in C programming language.

We know that in C if we want to save some value we take help of a variable.

So, a variable stores some value and we access that value by write the variable name.

In the following example we are storing an integer value in a variable num and to access that value we are writing the name of the variable.

// assign value to variable
int num = 10;

// access value stored in the variable
printf("%d", num);

Three things happen when we create a variable and assign a value to it.

  • A memory location is allocated to the variable.
  • The value assigned to the variable is stored in that allocated memory location.
  • The allocated memory location is referred by the name of the variable.

What are Pointers?

Pointers are derived data types and they store memory address.

So, pointers are variables that store memory addresses which are memory location of other variables.

Creating pointer variables

Before we can use a pointer variable we first have to create or declare them like any other variable in C programming.

Syntax of a pointer variable declaration is given below.

dataType *pointerVar;

In the above syntax the * asterisk indicates that we are creating a pointer variable.

The name of the pointer variable in the above syntax is pointerVar.

The created pointer variable pointerVar will point at the variable of type dataType.

Example

In the following example we are creating an integer pointer variable which will point at a variable of type int.

int *ptr;

Similarly, we can create a character pointer variable which will point at a variable of type char.

char *ptr;

Address of operator &

We use the address of & operator to get the address of a variable.

In the following syntax we will get the address of a variable varName.

&varName

Value at the address of operator *

We use the value at the address of * operator to get the value stored at any given address of a variable.

In the following syntax we will get the value stored at the given address.

*varName

Example

In the given example we are printing the address of the variable num and value stored at the address of the variable num.

#include <stdio.h>

int main(void) {
  // creating variable num
  int num;

  // assigning value to num
  num = 10;

  // printing the value stored in variable num
  printf("Value in num: %d\n", num);

  // printing the address of the variable num
  printf("Address of num: %ld\n", &num);
  
  // printing the value stored at the address of the variable num
  printf("Value at the address of num: %d\n", *(&num));
  
  return 0;
}

Note! In the above code &num gives us the address of variable num and *(&num) gives us the value stored at the address of the variable num.

Output:

Value in num: 10
Address of num: 140732733818936
Value at the address of num: 10

Garbage value in pointer variables

By default, when we declare a pointer variable it gets initialized by a random value a.k.a. garbage value.

In the following example we have created a pointer variable of type int. Since we have not assigned any address of any int type variable so, it holds some garbage value.

#include <stdio.h>

int main(void) {
  
  // pointer variable of type int
  int *ptr;
  
  // by default pointer variable ptr
  // holds garbage value as no
  // int variable address was assigned
  printf("Value stored in ptr: %ld\n", ptr);
  
  return 0;
}

Output:

Value stored in ptr: 140732859951192

We generally initialise a pointer variable to NULL value to avoid the random garbage value.

#include <stdio.h>

int main(void) {
  
  // pointer variable of type int
  int *ptr = NULL;
  
  printf("Value stored in ptr: %ld\n", ptr);
  
  return 0;
}

Output:

Value stored in ptr: 0

Initialising pointer variable with address

To initialise pointer variable with the address of another variable we use the & address of operator.

In the following example we have an integer variable num that holds value 10.

We also have a ptr pointer variable that holds the value of integer variable num. So, ptr variable is pointing at the num variable.

#include <stdio.h>

int main(void) {
  
  // num variable
  int num = 10;
  
  // pointer variable
  int *ptr;
  
  // assign address of num variable to ptr pointer variable
  ptr = &num;
  
  // print the value stored in num using the num variable
  printf("Value stored in num: %d\n", num);
  
  // print address of num stored in ptr variable
  printf("Address of num variable: %ld\n", ptr);
  
  // print address of ptr variable
  printf("Address of ptr variable: %ld\n", &ptr);
  
  // print the value stored in num using ptr variable
  printf("Value stored in num using ptr variable: %d\n", *ptr);
  
  return 0;
}

Output:

Value stored in num: 10
Address of num variable: 140732844468280
Address of ptr variable: 140732844468272
Value stored in num using ptr variable: 10

In the above image we are showing only the last 4 digits of the address.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT