Pointers

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

CPS Module 5-Structures, Pointers and Preprocessors

POINTERS
Pointers:

 Pointer is a derived data type in C.


 Pointers contain memory addresses as their values.
 Pointers can be used to access and manipulate data stored in the memory.

Benefits of using a pointer

 Pointers reduce the length and complexity of a program.


 They increase execution speed.
 A pointer enables us to access a variable that is defined outside the function.
 Pointers are more efficient in handling the data tables.
 The use of a pointer array of character strings results in saving of data storage space in memory.
 Pointers allow C to support dynamic memory management.
 Pointers provide an efficient tool for manipulating dynamic data structures such as structures,
linked lists, queues, stacks and trees.

Understanding a pointer
 A pointer is a variable that contains an address which is a location of another variable in memory.
 Since a pointer is a variable, its value is also stored in the memory in another location.
 Suppose we assign the address of quantity to a variable p.
 The link between the variables p and quantity can be visualized as shown in the figure.

 The address of p is 5048. Since the value of variable p is the address of the variable quantity, we
may access the value of a quantity by using the value of p and therefore, the variable p points to
the variable quantity. Thus, p gets the name ‘pointer’.

Accessing the Address of a variable


 Whenever a variable is defined in C language, a memory location is assigned for it, in which it's
value will be stored. We can easily check this memory address, using the & symbol.
 If var is the name of the variable, then &var will give it's address.

Example: Program to print the values and address of the variables


main()

Dept. of CSE, VVCE, Mysuru Page 1


CPS Module 5-Structures, Pointers and Preprocessors

{
char a;
int x;
float p,q;

a=’A’;
x=125;
p=10.25,q=18.76;
printf(“%c is stored at address %u. \n”,a,&a);
printf(“%d is stored at address %u. \n”,x,&x);
printf(“%f is stored at address %u. \n”,p,&p);
printf(“%f is stored at address %u. \n”,q,&q);
}

Output:
A is stored at address 4436
125 is stored at address 4434
10.25 is stored at address 4442
18.76 is stored at address 4438

Declaring a pointer variable:

Syntax:
data_type *pt_name

 The (*) asterisk tells that the variable pt_name is a pointer variable.
 Pt_name needs memory location
 Pt_name points to a variable of type data_type

Example:

1) int *p; //integer pointer


This declares the variable p as a pointer that points to an integer data type.

2) float *x; //float pointer


This declares the variable x as a pointer that points to a floating point type.
The pointer variable can be declared in any of the following ways:
1) int* p;
2) int *p;

Initializing the pointer variable:

 Declare a data variable


 Declare a pointer variable
 Assign the address of a variable to pointer variable using & operator and assignment operator

Dept. of CSE, VVCE, Mysuru Page 2


CPS Module 5-Structures, Pointers and Preprocessors

Method1:

int x; //declare a data variable


int *px; //declare a pointer variable
px=&x // copy the address of data variable to pointer variable

Method2:
int x; //declare a data variable
int *px=&x; //assign the address of data variable to pointer variable

Method3:
int x,*px=&x; //declare data variable and assign address

Consider the following declarations:

float a,b;
int x, *p;
p=&a; //wrong
b=*p;

The above declarations will result in erroneous output because we are attempting to assign the address of
float variable to an integer pointer.

 Pointers are flexible : we can make the pointer point to different data variables in different
statements
 Example:
int x,y,z,*p;
X Y Z
p=&x;
p=&y;
p=&z;

p
 Different pointers can be used to point to the same variable.
Example:
int x;
int *p1=&x; p1 p2 p3
int *p2=&x;
int *p3=&x;

Dept. of CSE, VVCE, Mysuru Page 3


CPS Module 5-Structures, Pointers and Preprocessors

Accessing a variable through its pointer:

As a part of declaration of the pointer variable, we use *. This symbol can also be used as an operator, called as
dereferencing operator or indirection operator. The notation *p means the object that p points to or the
location whose address is stored in p. This operator allows us to access and change the value stored at that
location.

address of a value of a

p a int a= 15;
92 15
int *p;
address 98 address 92
p= &a;
Consider the following set of assignments:

int quantity, *p,n;


quantity=179;
p=&quantity;
n=*p;
 The first line declares quantity and n as integer variables and p as a pointer variable pointing to an
integer. The second line assigns 179 to variable quantity and third line assigns the address of quantity to
pointer variable p. The fourth line contains the indirection operator *.
 The *operator placed before a pointer variable in an expression return the value of the variable.
 The * can be remembered as the “value at address”
 The value of n would be 179 with the following two statements
p=&quantity;
n=*p;
 The above statement is equivalent to
o n=*&quantity
 which is equivalent to
o n=quantity;
Example :

Dept. of CSE, VVCE, Mysuru Page 4


CPS Module 5-Structures, Pointers and Preprocessors

Chain of Pointers

 It is possible to make a pointer to point to another pointer, thus creating a chain of pointers as shown.

p2 p1 variable

Address2 Address1 value

 Here pointer variable p2 contains the address of the pointer variable p1, which points to the location
that contains the desired value. This is known as multiple indirections.
 A variable that is a pointer to a pointer must be declared using additional indirection operator symbol in
front of the name

Example:

int **p2;

 The pointer p2 is not an integer, but rather a pointer to an integer pointer.

Consider the following example:

Dept. of CSE, VVCE, Mysuru Page 5


CPS Module 5-Structures, Pointers and Preprocessors

Example Program:

Output:

Pointer Expressions:

 Like other variables pointers can also be used in expressions.

Example:

Dept. of CSE, VVCE, Mysuru Page 6


CPS Module 5-Structures, Pointers and Preprocessors

y =*p1**p2; same as (*p1) * (*p2)


sum=sum+*p1;
z=5*-*p2/ *p1; same as (5 * (-(*p2))) / (*p1)
*p2=*p2+10;
*p1=*p1+*p2;
*p1=*p2-*p1;
 C allows us to add integers to or subtract integers from pointers, as well as to substract one pointer
from another.

p1+4

p2-2

p1-p2

 If p1 and p2 are both pointers to the same array then p2-p1 gives the number of elements between p1
and p2.
 We may also use short hand operators with pointers
p1++;

-p2;

sum+=*p2;

 Pointers can also be compared using relational operators such as

p1>p2

p1==p2

p1!=p2

 We cannot use division, multiplication and addition of pointers


p1/p2
p1*p2
p1+p2 //are all illegal

Dept. of CSE, VVCE, Mysuru Page 7


CPS Module 5-Structures, Pointers and Preprocessors

Example Program:

main()
{
int a,b,*p1,*p2,x,y,z;
a=12;
b=4;
p1=&a;
p2=&b;
x=*p1 * *p2 - 6;
y=4* -*p2/*p1 +10;
printf(“Address of a =%u\n”,p1);
printf(“Address of b=%u\n”,p2);
printf(“\n”);
printf(“a =%d, b=%d\n”,a,b);
printf(“x=%d, y=%d \n”,x,y);
*p2=*p2 +3;
*p1=*p2 -5;
z=*p1 * *p2 -6;
printf(“a =%d, b=%d\n”,a,b);
printf(“z=%d\n”,z);
}

Output:
Address of a=4020
Address of b=4016
a=12,b=4
x=42,y=9
a=2,b=7,z=8

Operations performed on Pointers:

1. A pointer variable can be assigned the address of another variable.


2. A pointer variable can be assigned the values of another variable.
3. A pointer variable can be initialized with NULL or zero value.
4. A pointer variable can be prefixed or post fixed with increment or decrement operators.
5. An integer value may be added or subtracted from the pointer variable.
6. When two pointers point to the same array, one pointer variable can be subtracted from another.
7. When two pointers point to the objects of the same data types, they can be compared using relational
operators.
8. A pointer variable cannot be multiplied by a constant.
9. Two pointer variables cannot be added.
10. A value cannot be assigned to an arbitrary address(i.e. &x=10; is illegal)

Dept. of CSE, VVCE, Mysuru Page 8


CPS Module 5-Structures, Pointers and Preprocessors

Pointers and Arrays:

 Suppose we declare an array x as follows:

int x[5]={1,2,3,4,5};

 Suppose the base address of x is 1000 and assuming that each integer requires two bytes, the five
elements will be stored as follows:
x[0] x[1] x[2] x[3] x[4]
Elements

Value 1 2 3 4 5

Address 1000 1002 1004 1006 1008

Base Address

 The name x is defined as a constant pointer to the first element, x[0] and therefore the value of x is
1000, the location where x[0] is stored, that is

X= &x[0]=1000

 If we declare p as an integer pointer, then we can make the pointer p to point to the array x by the
following assignment:

p=x;

 This is equivalent to

p=&x[0];

 Now we can access every value of x using p++ to move from one element to another. The relationship
between p and x is as shown :

p=&x[0](=1000)
p+1=&x[1](=1002)
p+2=&x[2](=1004)
p+3=&x[3](=1006)
p+4=&x[4](=1008)

 Address of an element is calculated using its index and the scale factor of the data type:

Address of x[3]=base address +(3xscale factor of int)


= 1000 +(3*2)=1006

Example Program:
Program to find the sum of all the elements in an array using pointers

Dept. of CSE, VVCE, Mysuru Page 9


CPS Module 5-Structures, Pointers and Preprocessors

Output:

Pointers to two dimensional arrays:

 We know that the name of the array is a constant pointer that points to the 0th element of the array.
 In the case of a 2-D array, 0th element is a 1-D array.
 So the name of the array in case of a 2-D array represents a pointer to the 0th 1-D array. Therefore in
this case arr is a pointer to an array of 4 elements.
 If the address of the 0th 1-D is 2000, then according to pointer arithmetic (arr + 1) will represent the
address 2016, similarly (arr + 2) will represent the address 2032.

 From the above discussion, we can conclude that:


arr points to 0th 1-D array.
(arr + 1) points to 1st 1-D array.
(arr + 2) points to 2nd 1-D array

Dept. of CSE, VVCE, Mysuru Page 10


CPS Module 5-Structures, Pointers and Preprocessors

 Since *(arr + i) points to the base address of every ith 1-D array and it is of base type pointer to int, by
using pointer arithmetic we should we able to access elements of ith 1-D array.
 *(arr + i) points to the address of the 0th element of the 1-D array. So,
*(arr + i) + 1 points to the address of the 1st element of the 1-D array
*(arr + i) + 2 points to the address of the 2nd element of the 1-D array
 Hence we can conclude that:
*(arr + i) + j points to the base address of jth element of ith 1-D array

Pointers to Character arrays:

 The Consider the following example


char arr[] = "Hello World"; // array version
char ptr* = "Hello World"; // pointer version

 Here are the differences:


arr is an array of 12 characters.
When compiler sees the statement: char arr[] = "Hello World"
 It allocates 12 consecutive bytes of memory and associates the address of the first allocated byte
with arr

 On the other hand when the compiler sees the statement.


 char ptr* = "Hello World";
 It allocates 12 consecutive bytes for string literal "Hello World" and 4 extra bytes for pointer variable ptr.
And assigns the address of the string literal to ptr. So, in this case, a total of 14 bytes are allocated.

Dept. of CSE, VVCE, Mysuru Page 11


CPS Module 5-Structures, Pointers and Preprocessors

Example Program:
Program using pointers to determine the length of a character string

main()
{
char *name;
int length;
char *cptr=”name”;
name=”delhi”;
printf(“%s\n”,name);
while (*cptr!=’\0’)
{
printf(“%c is stored at address %u\n”,*cptr,cptr);
cptr++;
}
length=cptr-name;
printf(“\n Length of the string =%d\n”,length);
}

Output:
DELHI
D is stored at address 54
E is stored at address 55
L is stored at address 56
H is stored at address 57
I is stored at address 58

Length of the string = 5

Array of pointers:

 Just like we can declare an array of int, float or char etc, we can also declare an array of pointers, here is
the syntax to do the same.

Syntax: datatype *array_name[size];

Example:

int *arrop[5];

 Here arrop is an array of 5 integer pointers. It means that this array can hold the address of 5 integer
variables. In other words, you can assign 5 pointer variables of type pointer to int to the elements of this
array.

Dept. of CSE, VVCE, Mysuru Page 12


CPS Module 5-Structures, Pointers and Preprocessors

Example Program:

Output

Pointers and Functions:

Pointers as function Arguments:

 When an array is passed as an argument to functions, only the address of the first element of the array
is passed.
 The function uses this address for manipulating the array elements.
 Similarly we can pass the address of a variable as an argument to a function in the normal fashion. When
we pass the address of a variable as an argument the receiving parameter should be pointers. This
process of calling a function using pointers to pass the addresses of variables is known as “Call by
reference”.

Dept. of CSE, VVCE, Mysuru Page 13


CPS Module 5-Structures, Pointers and Preprocessors

Example Program:
Program to exchange the values stored in the two memory location using pointers and functions.

Output:

Functions returning Pointer variables:


 We have already seen a function can return data of types int , float, char etc. Similarly, a function can
return a pointer to data. The syntax of a function returning a pointer is as follows.

Syntax: type *function_name(type1, type2, ...);

 Some examples:
int *func(int, int); // this function returns a pointer to int
double *func(int, int); // this function returns a pointer to double

Dept. of CSE, VVCE, Mysuru Page 14


CPS Module 5-Structures, Pointers and Preprocessors

 Example Program:

Output:

Pointers and Structures:

 We have already learned that a pointer is a variable which points to the address of another variable of
any data type like int, char, float etc.
 Similarly, we can have a pointer to structures, where a pointer variable can point to the address of a
structure variable.
 Here is how we can declare a pointer to a structure variable.

struct dog
{
char name[10];
char breed[10];
int age;
char color[10];
};

struct dog spike;

// declaring a pointer to a structure of type struct dog


struct dog *ptr_dog

 This declares a pointer ptr_dog that can store the address of the variable of type struct dog. We can now assign
the address of variable spike to ptr_dog using & operator.
ptr_dog = &spike;

Dept. of CSE, VVCE, Mysuru Page 15


CPS Module 5-Structures, Pointers and Preprocessors

 Now ptr_dog points to the structure variable spike

Accessing members using Pointer


 There are two ways of accessing members of structure using pointer:
1. Using indirection (*) operator and dot (.) operator.
2. Using arrow (->) operator or membership operator.

Using Indirection (*) Operator and Dot (.) Operator

 At this point ptr_dog points to the structure variable spike, so by dereferencing it we will get the
contents of the spike. This means spike and *ptr_dog are functionally equivalent. For example:
(*ptr_dog).name – refers to the name of dog
(*ptr_dog).breed – refers to the breed of dog
and so on.

 Parentheses around *ptr_dog are necessary because the precedence of dot(.) operator is greater than
that of indirection (*) operator.

Using arrow operator (->)


 The above method of accessing members of the structure using pointers is slightly confusing and less
readable, that’s why C provides another way to access members using the arrow (->) operator.

 To access members using arrow (->) operator write pointer variable followed by -> operator, followed by
name of the member.
ptr_dog->name - refers to the name of dog
ptr_dog->breed - refers to the breed of dog
and so on

Example Program:

Dept. of CSE, VVCE, Mysuru Page 16


CPS Module 5-Structures, Pointers and Preprocessors

Output:

Dept. of CSE, VVCE, Mysuru Page 17


CPS Module 5-Structures, Pointers and Preprocessors

Preprocessors
 The preprocessor is a program that processes the source program before it is passed onto the compiler

 Any program execution needs the following steps.

Editor for writing the program.


Compilation
Linking
Executable code generation
 The preprocessor is another stage. The program passed in the editor is the source program /code to the
preprocessor. The preprocessor then passes it to the compiler. It is nonessential to write the program
with preprocessor facility. But it is a good practice to use it preferably at the beginning.

 One of the most important features of C language is to offer preprocessor directives. The preprocessor
directives are always initiated at the beginning and it starts with the symbol hash(#). It can be placed
anywhere but quite often it is declared before the main( ) function.

The #define Directive:

Preprocessing directive #define Syntax:

#define identifier <substitute text>

 substitute_string part is optional but, are used almost every time in program.

Example of #define
#define PI 3.14

 Here Pi is the macro template and 3.14 is the macro expansion.

C Program to find area of a circle. *Area of circle=πr2+

#include <stdio.h>
#define PI 3.1415
void main()
{
int radius;
float area;
printf("Enter the radius: ");
scanf("%d", &radius);
area=PI*radius*radius;
printf("Area=%.2f",area);
}

Dept. of CSE, VVCE, Mysuru Page 18


CPS Module 5-Structures, Pointers and Preprocessors

Output
Enter the radius: 3
Area=28.27

 In the above program PI replaces 3.14 during the program execution. In the program instead of writing
the value of PI as 3.14 we define directly the value of PI as 3.14.

There are three different forms of macro substitution:


1. Simple Macro substitution
2. Argumented Macro substitution
3. Nested macro substitution

Simple Macro substitution:

Simple string replacement is commonly used to define constants:

Examples:

#define COUNT 100


#define FALSE 0
#define SUBJECTS 6
#define PI 3.1415926

Macro can also include more than a simple constant value. It can include expressions as well.
#define AREA 5*12.46
#define SIZE sizeof(int)*4
#define D 45-22

Macro can also use definitions


#define TEST if(x>y)
#define PRINT printf(“Very Good \n”);

Macros with Arguments:

Syntax:
#define identifier(f1,f2,….fn) string

Examples:

#define CUBE(x) (x*x*x)


#define MAX(a,b) (((a)>(b))?(a): (b))
#define MIN(a,b) (((a)<(b))?(a): (b))
#define STREQ(s1,s2) (strcmp((s1,) (s2))==0)

Dept. of CSE, VVCE, Mysuru Page 19


CPS Module 5-Structures, Pointers and Preprocessors

Nesting of Macros:

 We can use one macro in the definition of another macro. That is macro definitions may be nested.

Examples:
#define M 5
#define N M+1
#define SQUARE(x) ((x)*(x))
#define CUBE(x) (SQUARE(x)*(x))

The #include Directive


 The #include directive loads the specified file in the current program. The macros and functions of
loaded file can be called the current program.
Syntax:
#include “filename”
#include<filename>
 Let us consider very common preprocessing directive as below:

#include <stdio.h>
 Here, "stdio.h" is a header file and the preprocessor replace the above line with the contents of header
file.
Example:

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“India is my country”);
}

O/P
India is my country

Compiler Control Directives


 The most frequently used conditional compilation directives are #if, #else, #endif etc. These directives
allow the programmer to include the portions of the codes based on the conditions. The complier
compiles selected portion of the source codes based on the condition.

Dept. of CSE, VVCE, Mysuru Page 20


CPS Module 5-Structures, Pointers and Preprocessors

The #ifdef and #endif directive:

The Syntax of #ifdef is given below:

#ifdef <identifier>
{
statement 1;
statement 2;
}
#else
{
statement 1;
statement 2;
}

#endif

Example 1:

#include<stdio.h>
#include<conio.h>
#define LINE 1
void main()
{
clrscr();
#ifdef LINE
printf(“This is line number one”);
#else
printf(“This is line number two”);
#endif
getch();
}

O/P
This is line number one

Example 2:

#include<stdio.h>
#include<conio.h>
#define E =
void main()
{
int a,b,c,d;
clrscr();
#ifdef E
{
a E 2;

Dept. of CSE, VVCE, Mysuru Page 21


CPS Module 5-Structures, Pointers and Preprocessors

b E 3;
printf(“A=%d, B=%d”, a,b);
}
#else
{
c=2;
d=3;
printf(“C=%d & D=%d”, c,d);
}
#endif
getch();
}

O/P
A=2 , B=3

The #ifndef and #else directive:

 The #ifndef works exactly opposite to that of #ifdef preprocessor tests whether the identifier has
defined substitute text or not.
 If identifier is defined then #else block is complied and executed and the compiler ignores #if block even
if errors are intentionally made.
 Error message will not be displayed.

 If identifier is not defined then #if block is compiled and executed.

The syntax of the #ifndef directive is as given below:

Syntax:

#ifndef<indeftifier>
{
statement 1;
statement 2;
}
#else
{
statement 1;
statement 2;
}
#endif

Example:

#include<stdio.h>
#include<conio.h>
#define T 8

Dept. of CSE, VVCE, Mysuru Page 22


CPS Module 5-Structures, Pointers and Preprocessors

void main()
{
clrscr( );
#ifndef T
printf(“\n Macro is not defined”);
#else
printf(“\n Macro is defined”);
#endif
}

O/P
macro is defined

The #error Directive:

The #error directive is used to display user defined message during compilation of the program.

The syntax is given below:

#if !defined (identifier)


#error <ERROR MESSAGE>
#endif

Example:
#include<stdio.h>
#include<conio.h>
#define B 1
void main()
{
clrscr();
#if!defined(A)
#error MACRO A IS NOT DEFINED
#else
printf(“Macro found”);
#endif
}

The #ifdefine directive will work exactly opposite to #! defined directive.

The syntax is given below:

#if define (identifier)


{
}
#else
#error <ERROR MESSAGE>
# endif

Dept. of CSE, VVCE, Mysuru Page 23


CPS Module 5-Structures, Pointers and Preprocessors

The #line directive:

The syntax of #line directive is as follows:

#line <constant> [<identifier>]

Causes the complier to imagine the line number of the next source line as given by <constant> and , <identifier>
gives the current input file. If identifier is absent, then the current file name remains unchanged.

Example:

#line 15 pragma.c

Dept. of CSE, VVCE, Mysuru Page 24

You might also like