1 of 21

Pointer

{C}

Programming

Programming for Problem Solving (PPS)

GTU # 3110003

USING

Prof. Nilesh Gambhava

Computer Engineering Department,�Darshan Institute of Engineering & Technology, Rajkot

2 of 21

  • The pointer in C language is a variable which stores the address of another variable.
  • This variable can be of type int, char, array, function, or any other pointer.

  • Consider the following example to define a pointer which stores the address of an integer.

int n = 10;   

int* p = &n; // Variable p of type pointer is pointing to the address of the variable n of type integer.   

3 of 21

Declaring a pointer�

  • The pointer in c language can be declared using * (asterisk symbol).

int *a; //pointer to int  

char *c; //pointer to char  

pointer variable stores the address of number variable, i.e., fff4. The value of number variable is 50. But the address of pointer variable p is aaa3.

4 of 21

#include<stdio.h>

int main()

{

int number=50;

int *p;

p=&number;//stores the address of number variable

printf("Address of p variable is %x \n",p);

printf("Value of p variable is %d \n",*p);

return 0;

}

O/P:

Address of p variable is fff4

Value of p variable is 50

5 of 21

Pointer to array

int arr[10];  

int *p[10]=&arr;  // Variable p of type pointer is pointing to the address of an integer array arr.  

Pointer to structure

struct st {  

    int i;  

    float f;  

}ref;  

struct st *p = &ref; 

6 of 21

Advantage of pointer�

1) Pointer reduces the code and improves the performance, it is used to retrieving strings, trees, etc. and used with arrays, structures, and functions.

2) We can return multiple values from a function using the pointer.

3) It makes you able to access any memory location in the computer's memory.

7 of 21

Usage of pointer�

There are many applications of pointers in c language.

1) Dynamic memory allocation

In c language, we can dynamically allocate memory using malloc() and calloc() functions where the pointer is used.

2) Arrays, Functions, and Structures

Pointers in c language are widely used in arrays, functions, and structures. It reduces the code and improves the performance.

8 of 21

Int* pc, c;

c = 5;

pc = &c;

Here, 5 is assigned to the c variable. And the address of c is assigned to the pc pointer.

To get the value of the thing pointed by the pointers, we use the * operator. For example:

int* pc, c;

c = 5;

pc = &c;

printf("%d", *pc); // Output: 5

9 of 21

What is Pointer?

  • A normal variable is used to store value.
  • A pointer is a variable that store address / reference of another variable.
  • Pointer is derived data type in C language.
  • A pointer contains the memory address of that variable as their value. Pointers are also called address variables because they contain the addresses of other variables.

Prof. Nilesh Gambhava

#3110003 (PPS) – Pointer

9

10 of 21

Pointers and Strings

  • Creating a pointer for the string
  • The variable name of the string str holds the address of the first element of the array i.e., it points at the starting memory address.
  • So, we can create a character pointer ptr and store the address of the string str variable in it. This way, ptr will point at the string str.
  • In the following code we are assigning the address of the string str to the pointer ptr.
  • char *ptr = str;

Prof. Nilesh Gambhava

#3110003 (PPS) – Pointer

10

11 of 21

Pointers and Strings

Prof. Nilesh Gambhava

#3110003 (PPS) – Pointer

11

12 of 21

Pointers and Strings

Prof. Nilesh Gambhava

#3110003 (PPS) – Pointer

12

13 of 21

Relation between Array & Pointer

  • When we declare an array, compiler allocates continuous blocks of memory so that all the elements of an array can be stored in that memory.
  • The address of first allocated byte or the address of first element is assigned to an array name.
  • Thus, array name works as pointer variable.
  • The address of first element is also known as base address.

Prof. Nilesh Gambhava

#3110003 (PPS) – Pointer

13

14 of 21

Relation between Array & Pointer – Cont.

  • Example: int a[10], *p;
  • a[0] is same as *(a+0), a[2] is same as *(a+2) and a[i] is same as *(a+i)

a[0]

a[1]

a[i]

a[9]

.

.

.

.

.

.

.

.

*(a+0)

*(a+1)

*(a+i)

*(a+9)

.

.

.

.

.

.

.

.

a:

a:

a+1:

a+i:

a+9:

2000

2002

2000 + i*2

2018

Prof. Nilesh Gambhava

#3110003 (PPS) – Pointer

14

15 of 21

Array of Pointer

  • As we have an array of char, int, float etc, same way we can have an array of pointer.
  • Individual elements of an array will store the address values.
  • So, an array is a collection of values of similar type. It can also be a collection of references of similar type known by single name.

datatype *name[size];

1

Syntax

int *ptr[5]; //declares an array of integer pointer of size 5

1

Example

Prof. Nilesh Gambhava

#3110003 (PPS) – Pointer

15

16 of 21

Array of Pointer – Cont.

  • An array of pointers ptr can be used to point to different rows of matrix as follow:

  • By dynamic memory allocation, we do not require to declare two-dimensional array, it can be created dynamically using array of pointers.

for(i=0; i<5; i++)

{

ptr[i]=&mat[i][0];

}

1

2

3

4

Example

ptr[0]

ptr[2]

ptr[1]

ptr[3]

ptr[4]

0

1

2

ptr

Prof. Nilesh Gambhava

#3110003 (PPS) – Pointer

16

17 of 21

Pointer and Function

  • Like normal variable, pointer variable can be passed as function argument and function can return pointer as well.
  • There are two approaches to passing argument to a function:
    • Call by value
    • Call by reference / address

Prof. Nilesh Gambhava

#3110003 (PPS) – Pointer

17

18 of 21

Call by value�

We can observe that even when we change the content of x and y in the scope of the swap function,

these changes do not reflect on x and y variables defined in the scope of main. This is because we call swap() by value and it will get separate memory for x and y so the changes made in swap() will not reflect in main().

Prof. Nilesh Gambhava

#3110003 (PPS) – Pointer

18

19 of 21

�� Call by reference / address��

We can observe in function parameters instead of using int x, int y we used int *x, int *y and in function call instead of giving x, y, we give &x, &y this methodology is call by reference as we used pointers as function parameter which will get original parameters' address instead of their value. & operator is used to give address of the variables and * is used to access the memory location that pointer is pointing. As the function variable is pointing to the same memory location as the original parameters, the changes made in swap() reflect in main() which we can see in the above output.

Prof. Nilesh Gambhava

#3110003 (PPS) – Pointer

19

20 of 21

Pointer to Function

  • Every function has reference or address, and if we know the reference or address of function, we can access the function using its reference or address.
  • This is the way of accessing function using pointer.

  • return-type: Type of value function will return.
  • argument list: Represents the type and number of value function will take, values are sent by the calling statement.
  • (*ptr-function): The parentheses around *ptr-function tells the compiler that it is pointer to function.
  • If we write *ptr-function without parentheses then it tells the compiler that ptr-function is a function that will return a pointer.

return-type (*ptr-function)(argument list);

1

Syntax

Prof. Nilesh Gambhava

#3110003 (PPS) – Pointer

20

21 of 21

#include<stdio.h>

int Sum(int,int);

int (*ptr)(int,int);

int main()

{

int a,b,rt;

printf("\nEnter 1st number : ");

scanf("%d",&a);

printf("\nEnter 2nd number : ");

scanf("%d",&b);

ptr = Sum;

rt = (*ptr)(a,b);

printf("\nThe sum is : %d",rt);

return 0;

}

int Sum(int x,int y)

{

return x + y;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

Enter 1st number : 5

Enter 2nd number : 10

The sum is : 15

Program

Output

Write a program to sum of two numbers using pointer to function.

Prof. Nilesh Gambhava

#3110003 (PPS) – Pointer

21