Program Compiled out in Turbo C Compiler...
3.Pointers :
3.1.Basic Pointers :
declaring an identifier along with * make it as pointer
datatype *identifier_name;
specifying a datatype is not for telling the compiler, how much size to be allocated to the identifier unlike a normal variable But it just specifies the type of aaddress that identifier can hold.
A pointer is allocated either 2 or 4 or ... bytes depending on the processor irrespective of the datatype
3.2Assinments to pointers:
pointer assignment with address of variable:
Once a pointer is declared it can hold the address of variable of that type.To retrieve address we use & operator.
like,
int *p;
int q;
p=&q;
pointer assignment with another pointer:
A pointer can be assigned to another pointer if they point to same datatype; if they point to different datatype,
a proper cast is needed. and retrieving the information from the pointer will retrieve the value present at that location.
pointer assignment with array :
A pointer and array are similar with some differences.
With Integers:
Consider,
int a[20]={1,2,3,4};
int *p;
p=a;
a++;//Not possible because a represents the base address and on incrementing it loses it's base address and it is constant
p++;//Possible and it can be made point to another array
int b[20]=[1,2,3,4};
a=b;//not possible as a is a constant
p=b;//possible
With Strings:
address of any charecter can be assigned to charecter pointer same as arrays.
char *p="that";
char s[]="this";
s="hhhh";//Not possible as s is constant
p="hhhh";//possible
s[0]='h';//possible as memory allocated for the string constant acts as volatile
*p='h';//not possible as memory aloocated for the string constant acts as const
With const keyword:
const char *p="this";//same as above p is not constant and it can point to other string constant but the data it point to cannot be altered like
*p='g';//not possible
p="that";//possible
*p='f';//again not possible
char *const p="that";
now p="this" will result in an error as p is made constant p cannot point to any other string constant.
Assignment with integers:
Only Zero is considered to be valid integer value for pointer. and a pointer can be assigned with 0.
Special Kind of pointers:
another type of pointer called void pointer can be declared.
void *p;
p is of type void meaning it point to nothing. pointer of any datatype canbe casted to this pointer and the resultant type and value will be of the one with which it is assigned with.
Arthemetics on pointers:
except +,- no other arthemetic operation can be performed on pointers.
the addition can be performed along with another integer constant and
the subraction can be performed along with integer constant and with another pointer if they point to same array.
Other operations:
all relational and logocal operations such as <,<=,>,>=,==,!= can be performed with pointer with another pointer. and also Comparision can be done between pointer and integer constant (Zero);
pointers to arrays:
a pointer can be made point to multi dimensional arrays.
ex: int (*p)[10];
p can point to two dimensional of [*][10] as we know , we can declare a array without specifing the row at initilization time and * indicates it can have any number of rows and a fixed number of column size of 10.same rule applicable to pointer with p point to two dimensional array column sixe 10 irrespective of row size.
ex: int (*p)[10]
Array of pointer
Pointers with functions:
the way a normal variable have address , functions will also have address abd we can have pointer to it.
Simple example of pointer pointing to a "printf" function
Syntax of printf:
int printf(char *,...);
so to point it,the pointer should also return same