Cps Pointers and Preprocessor Vtu Notes
Cps Pointers and Preprocessor Vtu Notes
Cps Pointers and Preprocessor Vtu Notes
POINTERS, PRE-PROCESSORS
A pointer is a variable that hold the address of another variable stored in elsewhere in the
computer memory. The pointer variable contains only the address of the referenced variable
not the value stored at that address. Pointers are generally used to point the variable such as
array, functions, structures, union and variables of any basic data type.
The two basic operators used with the pointer are,
The address of operator( & )
The indirection operator or dereference operator ( * ).
Dereferencing a pointer
Pointer dereference refers to the process of referencing the values of the variables by using
pointers indirectly. The contents of a variable can be accessed by dereferencing a pointer.
Example,
If a pointer ‘ p ’ is pointing to a variable ‘ num ‘ then its value can be accessed by saying
*p.
Output:
Number1 = 10
Number2 = 5
The address of memory-location num1 and num2 are passed to function and the
pointers *a and *b accept those values.
So, the pointer a and b points to address of num1 and num2 respectively.
When, the value of pointer is changed, the value in memory-location also changed
correspondingly.
Hence, change made to *a and *b was reflected in num1 and num2 in main function.
This technique is known as call by reference.
POINTERS AND STRUCUTRES
display(&stu);
}
void display(struct student *stud)
{
printf(“Student Details:\n”);
printf(“Student Name:%s\n”, stud->name);
printf(“Student Roll No:%d\n”, stud->rollno);
}
a
The name of the array always points to the first element of an array.
Here, address of first element of an array is &a[0].
Also, a represents the address of the pointer where it is pointing. Hence, &a[0] is
equivalent to a.
Also, value inside the address &a[0] and address a are equal.
Value in address &a[0] is a[0] and value in address a is *a. Hence, a[0] is equivalent
to *a.
Similarly,
&a[1] is equivalent to (a+1) AND, a[1] is equivalent to *(a+1).
&a[2] is equivalent to (a+2) AND, a[2] is equivalent to *(a+2).
&a[3] is equivalent to (a+1) AND, a[3] is equivalent to *(a+3).
.
.
&a[i] is equivalent to (a+i) AND, a[i] is equivalent to *(a+i).
Example: Program to find sum of all the elements of an array using pointers.
#include<stdio.h>
void main()
{
int i, a[10], n, sum=0;
printf("Enter the size of the array:");
scanf("%d", &n);
printf("Enter the elements into the array: ");
for(i=0;i<n;i++)
scanf("%d ",&a[i]);
for(i=0;i<n;i++)
sum+=*(a+i);
printf("Sum = %d ",sum);
}
Output:
Enter the size of the array:5
Enter the elements into the array: 2 4 6 10 15
Sum = 37
POINTER ARITHMETIC
As you know, pointer is an address, which is a numeric value.
Therefore, you can perform arithmetic operations on a pointer just as you can a
numeric value.
There are 4 arithmetic operators that can be used on pointers: ++, --, +, and –
To understand pointer arithmetic, let us consider that ptr is an integer pointer which
points to the address 1000.
Assuming 16-bit integers, let us perform the following arithmetic operation on the
pointer: ptr++
Now, after the above operation, the ptr will point to the location 1002 because each
time ptr is incremented, it will point to the next integer location which is 2 bytes next
to the current location.
Incrementing a Pointer
We prefer using a pointer in our program instead of an array because the variable pointer can
be incremented, unlike the array name which cannot be incremented because it is a constant
pointer.
Example: Program to increment the variable pointer to access each succeeding element of the
array.
#include<stdio.h>
void main()
{
int var[] = {10, 100, 200};
int i, *ptr;
ptr = var;
for ( i = 0; i < 3; i++)
{
printf("Address of var[%d] = %x \n", i, ptr );
printf("Value of var[%d] = %d \n", i, *ptr );
ptr++; //move to the next location
}
}
Output:
Address of var[0] = 1130
Value of var[0] = 10
Address of var[1] = 1132
Value of var[1] = 100
Address of var[2] = 1134
Value of var[2] = 200
POINTERS TO POINTERS
A variable which contains address of a pointer-variable is called pointer to a pointer.
PREPROCESSOR
A preprocessor is not part of the compiler, but is a separate step in the compilation
process.
In simplistic terms, a preprocessor is just a text substitution tool and they instruct
compiler to do required pre-processing before actual compilation.
All preprocessor commands begin with a pound symbol(#).
Use of #include
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: Program to illustrate use of #include
#include <stdio.h>
int main()
{
printf("SJEC
Mangalore"); return 0;
}
Output:
SJEC Mangalore
Use of #define
Analyze following examples to understand this directive.
#define PI 3.1415
The string 3.1415 is replaced in every occurrence of symbolic constant PI.
Example: Program to find area of a circle using #define.
#include<stdio.h>
#define PI 3.1415
int main()
{
int radius;
float area;
printf("Enter the radius: ");
scanf("%d", &radius);
area=PI*radius*radius;
printf("Area=%.2f",area);
return 0;
}
Output:
Enter the radius: 3
Area=28.27
Use of #error
The #error directives allow instructing the compiler to generate an error.
For example, we wish to issue a warning when a symbol is defined in the current
project.
Example: Program to illustrate use of #error.
#include<stdio.h>
#define MAX 100
void main()
{
#if(MAX)
#error: MAX is defined by me!!!
..
..
..
#endif
}
Output:
#error: MAX is defined by me!!!
Use of #undef
Consider an example shown below:
#undef FILE_SIZE
#define FILE_SIZE 42
This tells the preprocessor to undefine existing FILE_SIZE and define it as 42.