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 Functions - Call by Value and Call by Reference

C Programming

In this tutorial we will learn to use pointers with functions in C programming language.

We covered functions in the previous tutorials. Feel free to check that out.

Create a variable

We will start by creating an integer variable num.

#include <stdio.h>

int main(void) {
    
  // integer variable
  int num = 10;
  
  return 0;
}

We can represent this in memory as follows.

Passing value to function

There are two ways we can pass value to a function.

Call by value

In this method we pass a copy of the variable and not the actual variable to the called function. So, any change made to the copy of the variable in the called function is not reflected back to the original variable.

In the following example we have the num integer variable. We are calling the add10() function and passing the num variable as an argument to the function.

#include <stdio.h>

// function declaration
void add10(int);

int main(void) {
    
  // integer variable
  int num = 10;
  
  // print value of num
  printf("Value of num before function call: %d\n", num);
  
  // pass by value
  add10(num);
  
  // print value of num
  printf("Value of num after function call: %d\n", num);
  
  return 0;
}

// function definition
void add10(int n) {
  n = n + 10;
  printf("Inside add10(): Value %d\n", n);
}

Output:

Value of num before function call: 10
Inside add10(): Value 20
Value of num after function call: 10

We can see that the value of num is not changed after the function call. This is because when we pass the num variable by value as argument to the function add10() then, inside the add10() function we work with a copy n and not with the actual variable num.

In the following image we can see that num variable is saved in memory location 1000 and the parameter variable n of the function add10() gets stored at memory location 2000.

When num is passed to the function, the value of num i.e., 10 is saved in the location 2000.

And then we add 10 to n inside the add10() function. So, the new value at location 2000 becomes 20. But the value at location 1000 where the num variable is stored still remains 10. Hence we get the above result were num remains unchanged.

If we want to work with the variable num then we have to take help of pointer and pass num by reference.

Function taking pointers as argument

Following is the declaration syntax of a function to take pointers as argument.

returnType functionName(dataType *ptrVar);

Where returnType is the return type of the function. So, if the function will return no value then set it to void.

functionName is the name of the function.

dataType represents the data type of the pointer variable ptrVar.

Call by reference

In this case we pass the address of the variable num as argument to the add10() function.

#include <stdio.h>

// function declaration
void add10(int *);

int main(void) {
    
  // integer variable
  int num = 10;
  
  // print value of num
  printf("Value of num before function call: %d\n", num);
  
  // pass by reference
  add10(&num);
  
  // print value of num
  printf("Value of num after function call: %d\n", num);
  
  return 0;
}

// function definition
void add10(int *n) {
  *n = *n + 10;
  printf("Inside add10(): Value %d\n", *n);
}

Output:

Value of num before function call: 10
Inside add10(): Value 20
Value of num after function call: 20

We can see that in the above output the value of num gets changed.

In the following image we can see that the num variable is stored at memory location 1000.

When we are calling the add10() function we are passing the address of num to the function as argument.

So, the function parameter variable n which is allocated the memory location 2000, stores the address of the num variable i.e., 1000. So, n is pointing at num.

Inside the add10() function we are adding 10 using the following code.

*n = *n + 10;

Where, *n means the value at the address stored in the pointer variable n.

So, address stored inside pointer variable n is 1000 which points at the variable num. So, *n gives us 10 i.e., value of num.

Since we are updating the value at the location 1000 from inside the add10() function so, when we return back from the function call we get the updated value of num variable.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT