08-08-2012, 02:40 PM
POINTERS
POINTERS.pptx (Size: 3.89 MB / Downloads: 35)
a pointer is a variable that points to or references a memory location in which data is stored.
Pointers are variables used to store the address of another variable
Each memory cell in the computer has an address that can be used to access that location so a pointer variable points to a memory location we can access and change the contents of this memory location via the pointer.
Why pointer needed
Pointers can be used to
Call by address
Return more than 1 value from a function
Pass arrays and strings more conveniently from one function to another
Manipulate arrays more easily by moving pointers to them instead of moving the arrays themselves
Create complex data structures such as linked list and binary trees
Communicate information about memory
Compile faster,more efficient code than other derived data types such arrays
Pointer declaration
A pointer variable must be declared before it can be used.
Pointer variable is declared by preceding its name with an asterisk
The declaration of pointers follows this format: type * variable name where type is the data type of the value that the pointer is intended to point to. The asterisk tells the compiler that you are creating a pointer variable. Finally you give the name of the variable.
Null pointer
A null pointer is a special pointer ,value that is known (generally takes a value as zero),not to point anywhere.
NULL pointer is a pointer which is not pointing to any valid memory address.
NULL is macro constant which has been defined in several header file including stdio.h, alloc.h,mem.h, stdlib.h as
#define NULL 0
We can initialize a pointer to null pointer as
int *ptr=NULL;
We can also use constant value 0
int *ptr=0;
Passing pointers to function
When an argument is passed by reference, (i.e., when a pointer is passed to a function), the address of a data item is passed to the function.
When passing pointer as arguments to a function formal pointer arguments that must each be preceded by an asterisk.
Function prototypes are written in the same manner. If a function declaration does not include variable names, the data type of each pointer argument must be followed by an asterisk.