Pointers Pointers are the variables that contain the
Pointers �Pointers are the variables that contain the address of another variable within the memory.
Pointer Declaration �Syntax: data_type *ptvar �data_type- datatype of the pointer’s object. �(*)- indicates that this variable is a pointer. �ptvar- name of the pointer variable. �This statement tells the compilers that: ptvar is a pointer. ptvar needs memory location ptvar points to a variable of type data_type Eg: int *p; float *abc; char *status;
Pointer Initialization �To initialize the pointer variable we use the address(&) operator as a prefix to the variable name assigns its address to the pointer. �Eg: iptr= &ivar //assigns the address of variable ‘ivar’ to pointer ‘iptr’.
�We can also assign a pointer value to another variable of the same type. �Eg: int a=5, x, *b; b=&a; //assigns the address of variable ‘a’ to pointer ‘b’. x=*b; //assigns the value at address pointed by ‘b’ to variable x i. e x=5. *b=0 //assigns value 0 to ‘a’ since a=*aptr.
Void Pointer �Void pointer is a generic type that is not associated with a reference type that is not an integer, a character or any other type. �It is compatible for assignment purpose only with all other pointer types. �This a pointer of any reference type can be assigned to a pointer of any reference type. �Restriction is that void pointer has no object type it cannot be dereference unless it is cast �Eg: void *add;
�Null pointer is not same as the void pointer. �Null pointer is a pointer of any type that is assigned the constant NULL. �The reference type of pointer will not change with the null assignment. �A variable of void pointer is pointer with no reference type that can stored only the address of any variable. �Eg: void *add; //void pointer �Int *adda=NULL; //NULL pointer of type int.
- Slides: 6