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 and Variables

C Programming

In this tutorial we will learn to manipulate variables using pointers in C programming language.

We already know from the Pointers tutorial how to create a pointer variable and store address of a variable in it.

Now let us go ahead and create an integer int variable and manipulate it via an integer pointer variable ptr.

Creating an integer variable

This is fairly simple. All we have to do is declare a variable using the int data type.

int num = 10;

Three things will happen for the above line of code.

  • A memory location is located to store integer value.
  • The value 10 is saved in that memory location.
  • We can refer the memory location using the variable name num.

Creating integer pointer variable

Now we will create an integer pointer variable ptr that will store the address of the integer variable num.

To get the address of a variable we use the address of & operator.

int *ptr = #

We can represent the integer variable num and pointer variable ptr as follows.

So, we can see that variable num was allocated memory location 8280 (which will change the next time the program is executed). In that location value 10 was stored.

Similarly, variable ptr was allocated memory location 8272 and it holds the value 8280 which is the memory location of variable num. So, ptr is pointing at the num variable.

Accessing value of a variable via pointer

To access the value stored in the variable num via the pointer variable ptr we have to use the value at the address of * operator.

We already have the address of variable num stored in variable ptr. So, using *ptr will give use the value stored at the address i.e., value stored in num variable.

//value of num via ptr
printf("num value via ptr: %d\n", *ptr);

Updating the value of a variable via pointer

To update the value of a variable via pointer we have to first get the address of the variable and then set the new value in that memory address.

We get the address via address of & operator and then we set the value using the value at the address of * operator.

// updating the value of num via ptr
*ptr = 20;

Complete code:

#include <stdio.h>

int main(void) {
  
  // num variable
  int num = 10;

  // ptr pointer variable
  int *ptr = NULL;

  // assigning the address of num to ptr
  ptr = &num;

  // printing the value of num - Output: 10
  printf("num: %d\n", num);
  printf("num via ptr: %d\n", *ptr);
 
  // updating the value of num via ptr
  printf("Updating value of num via ptr...\n");
  *ptr = 20;

  // printing the new value of num - Output: 20
  printf("num: %d\n", num);
  printf("num via ptr: %d\n", *ptr);

  return 0;
}

Output:

num: 10
num via ptr: 10
Updating value of num via ptr...
num: 20
num via ptr: 20

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT