Pps Using C r20 - Unit-4
Pps Using C r20 - Unit-4
Pps Using C r20 - Unit-4
UNIT-IV
Pointers: Introduction, Pointers to pointers, Compatibility, L value and R value, Tips and
Common Programming Errors, Key Terms, Summary, Practice Set.
Pointer Applications: Arrays, and Pointers, Pointer Arithmetic and Arrays, Memory
Allocation Function, Array of Pointers, Programming Application, Tips and Common
Programming Errors, Key Terms, Summary, Practice Set.
Processor Commands: Processor Commands, Tips and Common Programming Errors, Key
Terms, Summary, Practice Set.
Introduction:-
Advantages of Pointers:-
1. A pointer enables us to access a variable that is defined out side the function.
2. Pointers are more efficient in handling the data tables.
3. Pointers reduce the length and complexity of a program.
4. They increase the execution speed.
Definition :-
A variable that holds a physical memory address is called a pointer variable or
Pointer
Declaration :
Datatype * Variable-name;
www.Jntufastupdates.com 1
A pointer is a variable that contains an address which is a location of
another variable in memory.
Pointers to pointers:-
A pointer to a pointer is a form of multiple indirection, or a chain of
pointers. Normally, a pointer contains the address of a variable.
When we define a pointer to a pointer, the first pointer contains the
address of the second pointer, which points to the location that contains
the actual value as shown below.
www.Jntufastupdates.com 2
This is done by placing an additional asterisk in front of its
name. For example, the following declaration declares a
pointer to a pointer of type int –
int **var;
Compatibility pointer:-
The rules for assigning one pointer to another are tighter than the
rules for numeric types.
For example, you can assign an int value to a double variable without
using a type conversion, but you can’t do the same for pointers to
these two types. Let’s see a simple C program to exemplify this.
*
* ptr_compatibility.c -- program illustrates concept of pointer
* compatibility
*/
#include <stdio.h>
int main(void)
{
int n = 5;
long double x;
return 0;
}
www.Jntufastupdates.com 3
1. he name of the variable of any type i.e, an identifier of integral,
floating, pointer, structure, or union type.
2. A subscript ([ ]) expression that does not evaluate to an array.
3. A unary-indirection (*) expression that does not refer to an array
4. An l-value expression in parentheses.
5. A const object (a nonmodifiable l-value).
6. The result of indirection through a pointer, provided that it isn’t a
function pointer.
7. The result of member access through pointer(-> or .)
Arrays:-
An array is defined as the collection of similar type of data
items stored at contiguous memory locations.
Arrays are the derived data type in C programming
language which can store the primitive type of data such
as int, char, double, float, etc.
It also has the capability to store the collection of derived
data types, such as pointers, structure, etc.
The array is the simplest data structure where each data
element can be randomly accessed by using its index
number.
Pointer Arithmetic in C:-
Pointer Arithmetic in C
We can perform arithmetic operations on the pointers like
addition, subtraction, etc.
However, as we know that pointer contains the address,
the result of an arithmetic operation performed on the
www.Jntufastupdates.com 4
pointer will also be a pointer if the other operand is of
type integer.
In pointer-from-pointer subtraction, the result will be an
integer value.
Following arithmetic operations are possible on the
pointer in C language:
o Increment
o Decrement
o Addition
o Subtraction
o Comparison
Decrementing Pointer in C
Like increment, we can decrement a pointer variable. If
we decrement a pointer, it will start pointing to the
previous location.
The formula of decrementing the pointer is given below:
C Pointer Addition
www.Jntufastupdates.com 5
C Pointer Subtraction
1. malloc()
2. calloc()
3. realloc()
4. free()
Now let's have a quick look at the methods used for dynamic memory
allocation.
www.Jntufastupdates.com 6
malloc() allocates single block of requested memory.
Array of Pointers :-
In computer programming, an array of pointers is an indexed
set of variables in which the variables are pointers (a reference
to a location in memory).
Pointers are an important tool in computer science for
creating, using, and destroying all types of data
structures.
An array of pointers is useful for the same reason that all
arrays are useful: it allows you to numerically index a
large set of variables.
Below is an array of pointers in C that sets each pointer in
one array to point to an integer in another and then print
the values of the integers by dereferencing the pointers.
www.Jntufastupdates.com 7
i.e., a and k& a[20] are equal
P = a;
We can access every value of array a by moving P from one element to another.
i.e., P points to 0th element
P+1 points to 1st element
P+2 points to 2nd element
P+3 points to 3rd element
P +4 points to 4th element
Programming Application:-
APPLICATIONS OF C LANGUAGE
Processor Commands:-
The C Preprocessor is not a part of the compiler, but is a
separate step in the compilation process.
In simple terms, a C Preprocessor is just a text substitution
tool and it instructs the compiler to do required pre-processing
before the actual compilation.
We'll refer to the C Preprocessor as CPP.
All preprocessor commands begin with a hash symbol (#).
It must be the first nonblank character, and for readability, a
preprocessor directive should begin in the first column.
The following section lists down all the important preprocessor
directives –
www.Jntufastupdates.com 8
Sr.No. Directive & Description
1 #define
Substitutes a preprocessor macro.
2 #include
Inserts a particular header from another file.
3 #undef
Undefines a preprocessor macro.
4 #ifdef
Returns true if this macro is defined.
5 #ifndef
Returns true if this macro is not defined.
6 #if
Tests if a compile time condition is true.
7 #else
The alternative for #if.
8 #elif
#else and #if in one statement.
9 #endif
Ends preprocessor conditional.
10 #error
Prints error message on stderr.
11 #pragma
Issues special commands to the compiler, using a standardized
method.
www.Jntufastupdates.com 9