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

Module4 Chapter2

Module notes chemistry
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Module4 Chapter2

Module notes chemistry
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Module 4

Pointers
4.6 Introduction
 “A pointer is a variable that holds the address of another variable”. or
 A pointer is a variable that contains the memory location of another variable. Therefore, a
pointer is a variable that represents the location of a data item, such as a variable or an array
element.

Applications of pointer
 Pointers are used to pass information back and forth between functions.
 Pointers enable the programmers to return multiple data items from a function via function arguments.
 Pointers provide an alternate way to access the individual elements of an array.
 Pointers are used to pass arrays and strings as function arguments.
 Pointers are used to create complex data structures, such as trees, linked lists, linked stacks, linked
queues, and graphs.
 Pointers are used for the dynamic memory allocation of a variable.

4.7 Declaring Pointer Variables


 Pointer provides access to a variable by using the address of that variable.
 A pointer variable is therefore a variable that stores the address of another variable.
 The general syntax of declaring pointer variables can be given as below.
data_type *ptr_name;
Here, data_type: is the data type of the value that the pointer will point to. It can be int, float, char etc.
Asterisk (*): It tells the compiler that we are declaring a pointer variable.
pointer_variable_name: It is the name of the pointer variable.

Example:
1. int *ptr; // declares a pointer variable ptr of integer type.
2. float *temp; // declares a pointer variable temp of floating type.

Operators used with Pointers:


The two basic operators used with pointers are:
i. The Address of operator (&): By using the address of (&) operator, we can determine the address of the
variable.
ii. The Indirection operator (*): It gives the value stored at a particular address.

Example:
int a=3;
int *ptr;
ptr=&a;

Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 1
ptr a
Memory layout:
65530 3
Address: 65530
 ‘ptr = &a’ copies the address of ‘a’ to the pointer variable ‘ptr’.

Example Program: Write a C program to print value and address of the variable using pointers.
#include<stdio.h> Output:
void main () The address of a=65530 and value of a=20
{
int a=20, *ptr;
ptr = &a; //ptr1 is a pointer to variable a
printf(“The address of a=%d and value of a=%d\n”,ptr,*ptr);
}
ptr1 a
Memory layout:
65530 20
Address: 65530

Initializing a Pointer Variable


 We can initialize the pointer variables by assigning the address of other variable to them. However these
variables must be declared in the program.

Syntax
data_type * ptr_name = address_of_variable;
where,
data_type:.It can be int, float, char etc.
Asterisk (*): It tells the compiler that we are declaring a pointer variable.
ptr_name: It is the name of the pointer variable.
address_of_variable: Itis the address of another variable.
Example:
int a;
int *ptr;
ptr=&a;
or
int a;
int *ptr=&a;
Both are equivalent.

 We can dereference a pointer, i.e., refer to the value of the variable to which it points, by using unary
'*' operator (also known as indirection operator) as *pnum, i.e., *pnum = 10, since 10 is value of x.
Therefore, * is equivalent to writing value at address. Look at the code below which shows the use of
pointer variable.
#include <stdio.h>
void main()
{
int num, *pnum;
pnum = &num;
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 2
printf(“Enter the number : ");
scanf("%d", &num);
printf("\n The number that was entered is : %d", *pnum);
}
Output:
Enter the number : 10
The number that was entered is : 10
What will be the value of *(&num)? It is equivalent to simply writing num.

4.8 Types of Pointer


4.8.1 Null Pointers
 We have studied that a pointer variable is a pointer to a variable of some data type.
 However, in some cases, we may prefer to have a null pointer which is a special pointer value and does
not point to any value.
 This means that a null pointer does not point to any valid memory address.
 To declare a null pointer, you may use the predefined constant NULL which is defined in several
standard header files including <stdio.h>, <stdlib.h> , and <string.h>.
 After including any of these files in your program, you can write
int *ptr = NULL;
 You can always check whether a given pointer variable stores the address of some variable or contains
NULL by writing,
if (ptr == NULL)
{
Statement block;
}
 You may also initialize a pointer as a null pointer by using the constant 0
int *ptr, ptr = 0;
 This is a valid statement in C as NULL is a preprocessor macro, which typically has the value or
replacement text 0. However, to avoid ambiguity, it is always better to use NULL to declare a null pointer.
A function that returns pointer values can return a null pointer when it is unable to perform its task.
 Null pointers are used in situations where one of the pointers in the program points to different
locations at different times.

4.8.2 Generic Pointers


 A generic pointer is a pointer variable that has void as its data type.
 The void pointer, or the generic pointer, is a special type of pointer that can point to variables of any
data type.
 It is declared like a normal pointer variable but using the void keyword as the pointer’s data type. For
example,
void *ptr;
 In C, since you cannot have a variable of type void, the void pointer will therefore not point to any data
and, thus, cannot be dereferenced. You need to cast a void pointer to another kind of pointer before
using it.
 Generic pointers are often used when you want a pointer to point to data of different types at different
times. For example,

Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 3
#include <stdio.h>
void main()
{
int x=10;
char ch = ‘A’;
void *gp;
gp = &x;
printf("\n Generic pointer points to the integer value = %d", *(int*)gp);
gp = &ch;
printf("\n Generic pointer now points to the character= %c", *(char*)gp);
}

Output:
Generic pointer points to the integer value = 10
Generic pointer now points to the character = A

4.9 Passing Arguments to Function Using Pointers


 Using call-by-value method, it is impossible to modify the actual parameters when you pass them to a
function. Furthermore, the incoming arguments to a function are treated as local variables in the function
and those local variables get a copy of the values passed from their calling function.
 Pointer provides a mechanism to modify data declared in one function using code written in another
function. In other words: If data is declared in func1() and we write code in func2() that modifies the data
in func1(), then we must pass the addresses of the variables to func2().
 The calling function sends the addresses of the variables and the called function declares those
incoming arguments as pointers. In order to modify the variables sent by the calling function, the called
function must dereference the pointers that were passed to it. Thus, passing pointers to a function avoids
the overhead of copying data from one function to another.
 Hence, to use pointers for passing arguments to a function, the programmer must do the following:
 Declare the function parameters as pointers.
 Use the referenced pointers in the function body.
 Pass the addresses as the actual argument when the function is called.

1. Write a C program to add two numbers using call by reference.


#include<stdio.h>

int add (int *a,int *b)


{
int sum;
sum = *a + *b;
return sum;
}

void main()
{
Output:
int a,b, res;
printf(“Enter the values of a and b:”); Enter the values of a and b: 4 5
scanf(“%d%d”,&a,&b); result =9
res = add(&a,&b);
printf(“result =%d\n”, res);
}

Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 4
2. Write a C program to swap two numbers using call by reference.
#include<stdio.h>
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void main()
{
int a,b;
printf(“Enter the values of a and b:”); Enter the values of a and b: 10 20
scanf(“%d%d”,&a,&b); Before swapping: a=10 b=20
printf(“Before swapping: a=%d\tb=%d”, a, b); After swapping: a=20 b=10
swap(&a,&b);
printf(“After swapping: a=%d\tb=%d”, a, b)
}

Output:
Enter the values of a and b: 10 20
Before swapping: a=10 b=20
After swapping: a=20 b=10

Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 5

inprotected.com

You might also like