0% found this document useful (0 votes)
38 views

UNIT 3

This document provides a comprehensive overview of pointers in the C programming language, explaining their definition, declaration, and usage. It covers various types of pointers including pointers to arrays, functions, structures, and void pointers, as well as concepts like pointer initialization, dereferencing, and double pointers. Additionally, it highlights the advantages of using pointers, such as dynamic memory allocation and improved performance in code execution.

Uploaded by

neelkadiya6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

UNIT 3

This document provides a comprehensive overview of pointers in the C programming language, explaining their definition, declaration, and usage. It covers various types of pointers including pointers to arrays, functions, structures, and void pointers, as well as concepts like pointer initialization, dereferencing, and double pointers. Additionally, it highlights the advantages of using pointers, such as dynamic memory allocation and improved performance in code execution.

Uploaded by

neelkadiya6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Unit 3

Pointers
 Introduction to Pointer
 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.
 The size of the pointer depends on the architecture.
 However, in 32-bit architecture the size of a pointer is 2 byte.
 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 ty
pe integer.
 Declaring a pointer
 The pointer in c language can be declared using * (asterisk symbol). It is also known as
indirection pointer used to dereference a pointer.
 int *a;//pointer to int
 char *c;//pointer to char
 Pointer Example

 As you can see in the above figure, 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.
 By the help of * (indirection operator), we can print the value of pointer variable p.
#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;
}
 Output

1
 Address of p variable is fff4
 Value of p variable is 50
 Pointer to array
 int arr[10];
 int *p[10]=&arr; // Variable p of type pointer is pointing to the address of an integer arr
ay arr.
 Pointer to a function
 void show (int);
 void(*p)(int) = &display; // Pointer p is pointing to the address of a function
 Pointer to structure
 struct st
 {
 int i;
 float f;
 }ref;
 struct st *p = &ref;


 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.
 Usage of pointer
 Dynamic memory allocation
 In c language, we can dynamically allocate memory using malloc() and calloc()
functions where the pointer is used.
 Arrays, Functions, and Structures
 Pointers in c language are widely used in arrays, functions, and structures. It reduces
the code and improves the performance.
 In the c programming language, we use normal variables to store user data values.
 When we declare a variable, the compiler allocates required memory with the specified
name.
 In the c programming language, every variable has a name, data type, value, storage class,
and address.
 We use a special type of variable called a pointer to store the address of another variable
with the same datatype.
 A pointer is defined as follows...

2
 Pointer is a special type of variable used to store the memory location address of a
variable.
 In the c programming language, we can create pointer variables of any datatype.
 Every pointer stores the address the variable with same datatype only.
 That means integer pointer is used store the address of integer variable only.

 Accessing the Address of Variables


 In c programming language, we use the reference operator "&" to access the address of
variable.
 For example, to access the address of a variable "marks" we use "&marks". We use the
following printf statement to display memory location address of variable "marks"...
 Example Code
 printf("Address : %u", &marks) ;
 In the above example statement %u is used to display address of marks variable. Address of
any memory location is unsigned integer value.

 Declaring Pointers (Creating Pointers)


 In c programming language, declaration of pointer variable is similar to the creation of
normal variable but the name is prefixed with * symbol.
 We use the following syntax to declare a pointer variable...
 datatype *pointerName ;
 A variable declaration prefixed with * symbol becomes a pointer variable.
 Example Code
 int *ptr ;
 In the above example declaration, the variable "ptr" is a pointer variable that can be used
to store any integer variable address.

 Assigning Address to Pointer


 To assign address to a pointer variable we use assignment operator with the following
syntax...
 pointerVariableName = & variableName ;
 For example, consider the following variables declaration.
 int a, *ptr ;
 In the above declaration, variable "a" is a normal integer variable and variable "ptr" is an
integer pointer variable.
 If we want to assign the address of variable "a" to pointer variable "ptr" we use the
following statement...
 Example Code
 ptr = &a ;
 In the above statement, the address of variable "a" is assigned to pointer variable "ptr".
Here we say that pointer variable ptr is pointing to variable a.

3
 Accessing Variable Value Using Pointer
 Pointer variables are used to store the address of other variables.
 We can use this address to access the value of the variable through its pointer.
 We use the symbol "*" in front of pointer variable name to access the value of variable to
which the pointer is pointing.
 We use the following general syntax...
 *pointerVariableName
 Example Code
#include<stdio.h>
#include<conio.h>
void main()
{
int a = 10, *ptr ;
clrscr();
ptr = &a ;
printf("Address of variable a = %u\n", ptr) ;
printf("Value of variable a = %d\n", *ptr) ;
printf("Address of variable ptr = %u\n", &ptr) ;
}

 Initialization of Pointer Variables


 Pointer Initialization is the process of assigning address of a variable to a pointer variable.
 It contains the address of a variable of the same data type. In C language address
operator & is used to determine the address of a variable.
 The & (immediately preceding a variable name) returns the address of the variable
associated with it.
 int a = 10;
 int *ptr; //pointer declaration
 ptr = &a; //pointer initialization

 Pointer variable always points to variables of the same datatype. For example:
 float a;

4
 int *ptr = &a; // ERROR, type mismatch
 While declaring a pointer variable, if it is not assigned to anything then it contains garbage
value. Therefore, it is recommended to assign a NULL value to it,

 A pointer that is assigned a NULL value is called a NULL pointer in C.


 int *ptr = NULL;
 Example Code
#include<stdio.h>
#include<conio.h>
void main()
{
int a = 10, *ptr ;
clrscr();
ptr = &a ;
printf("Address of variable a = %u\n", ptr) ;
printf("Value of variable a = %d\n", *ptr) ;
printf("Address of variable ptr = %u\n", &ptr) ;
}

 Using the pointer or Dereferencing of Pointer


 Once a pointer has been assigned the address of a variable, to access the value of the
variable, the pointer is dereferenced, using the indirection operator or dereferencing
operator *. Consider the following example for better understanding.
 Example:-
#include <stdio.h>
int main()
{
int a;
a = 10;
int *p = &a; // declaring and initializing the pointer
//prints the value of 'a'
printf("%d\n", *p);
printf("%d\n", *&a);

5
//prints the address of 'a'
printf("%u\n", &a);
printf("%u\n", p);
printf("%u\n", &p); //prints address of 'p'
return 0;
}
 output
10
10
3795480300
3795480300
3795480304

 Pointer and Arrays,


 An array is a block of sequential data. Let's write a program to print addresses of array
elements.
 #include <stdio.h>
int main()
{
int x[4];
int i;
for(i = 0; i < 4; ++i)
{
printf("&x[%d] = %p\n", i, &x[i]);
}
printf("Address of array x: %p", x);
return 0;
}
 Output
 &x[0] = 1450734448
 &x[1] = 1450734452
 &x[2] = 1450734456
 &x[3] = 1450734460
 Address of array x: 1450734448
 There is a difference of 4 bytes between two consecutive elements of array x. It is because
the size of int is 4 bytes (on our compiler).
 Notice that, the address of &x[0] and x is the same. It's because the variable name x points
to the first element of the array.

6
 Relation between Arrays and Pointers
 From the above example, it is clear that &x[0] is equivalent to x. And, x[0] is equivalent
to *x.
 Similarly,&x[1] is equivalent to x+1 and x[1] is equivalent to *(x+1).
 &x[2] is equivalent to x+2 and x[2] is equivalent to *(x+2)....
 Basically, &x[i] is equivalent to x+i and x[i] is equivalent to *(x+i).
 Example 1: Pointers and Arrays
#include <stdio.h>
int main()
{
int i, x[6], sum = 0;
printf("Enter 6 numbers: ");
for(i = 0; i < 6; ++i)
{
// Equivalent to scanf("%d", &x[i]);
scanf("%d", x+i);
// Equivalent to sum += x[i]
sum += *(x+i);
}
printf("Sum = %d", sum);
return 0;
}
 When you run the program, the output will be:
Enter 6 numbers: 2
3
4
4
12
4
Sum = 29

 pointer to pointer (C Double Pointer)


 As we know that, a pointer is used to store the address of a variable in C.
 Pointer reduces the access time of a variable.
 However, In C, we can also define a pointer to store the address of another pointer. Such
pointer is known as a double pointer (pointer to pointer).

7
 The first pointer is used to store the address of a variable whereas the second pointer is
used to store the address of the first pointer.
 Let's understand it by the diagram given below.

 The syntax of declaring a double pointer is given below.


 int **p; // pointer to a pointer which is pointing to an integer.
 Consider the following example.
#include<stdio.h>
void main ()
{
int a = 10;
int *p;
int **pp;
p = &a; // pointer p is pointing to the address of a
pp = &p; // pointer pp is a double pointer pointing to the address of pointer p
printf("address of a: %x\n",p);
printf("address of p: %x\n",pp);
printf("value stored at p: %d\n",*p);
printf("value stored at pp: %d\n",**pp);
}
 Output
address of a: d26a8734
address of p: d26a8738
value stored at p: 10
value stored at pp: 10

 void pointers
 Till now, we have studied that the address assigned to a pointer should be of the same type
as specified in the pointer declaration.
 For example, if we declare the int pointer, then this int pointer cannot point to the float
variable or some other type of variable, i.e., it can point to only int type variable.
 To overcome this problem, we use a pointer to void.
 A pointer to void means a generic pointer that can point to any data type.
 We can assign the address of any data type to the void pointer, and a void pointer can be
assigned to any type of the pointer without performing any explicit typecasting.
 Syntax of void pointer

8
 void *pointer name;
 Declaration of the void pointer is given below:
 void *ptr;
 In the above declaration, the void is the type of the pointer, and 'ptr' is the name of the
pointer.
 Size of the void pointer in C
 The size of the void pointer in C is the same as the size of the pointer of character type.
 According to C perception, the representation of a pointer to void is the same as the
pointer of character type.
 The size of the pointer will vary depending on the platform that you are using.
 Let's look at the below example:
#include <stdio.h>
int main()
{
void *ptr = NULL; //void pointer
printf("size of void pointer = %d\n\n",sizeof(ptr));
return 0;
}
 Advantages of void pointer
 The malloc() and calloc() function return the void pointer, so these functions can be
used to allocate the memory of any data type.
 The void pointer in C can also be used to implement the generic functions in C.

 Why we use void pointers?


 We use void pointers because of its reusability.
 Void pointers can store the object of any type, and we can retrieve the object of any
type by using the indirection operator with proper typecasting.
 Let's understand through an example.
#include<stdio.h>
int main()
{
int a=56; // initialization of a integer variable 'a'.
float b=4.5; // initialization of a float variable 'b'.
char c='k'; // initialization of a char variable 'c'.
void *ptr; // declaration of void pointer.
ptr=&a;
printf("value of 'a' is : %d",*((int*)ptr));
ptr=&b;
printf("\nvalue of 'b' is : %f",*((float*)ptr));
ptr=&c;
printf("\nvalue of 'c' is : %c",*((char*)ptr));

9
return 0;
}

 Pointer to functions
 It is possible to declare a pointer pointing to a function which can then be used as an
argument in another function. A pointer to a function is declared as follows,
 type (*pointer-name)(parameter);
 Here is an example :
 int (*sum)(); //legal declaration of pointer to function
 int *sum(); //This is not a declaration of pointer to function
 A function pointer can point to a specific function when it is assigned the name of that
function.
 int sum(int, int);
 int (*s)(int, int);
 s = sum;
 Here s is a pointer to a function sum. Now sum can be called using function pointer s along
with providing the required argument values.
 s (10, 20);
 Example of Pointer to Function
#include <stdio.h>
int sum(int x, int y)
{
return x+y;
}
int main( )
{
int (*fp)(int, int);
fp = sum;
int s = fp(10, 15);
printf("Sum is %d", s);

return 0;
}

10

You might also like