Tuesday, 12 March 2024

Lecture 2

 Pointers:-

Pointers are a type of variable that allow you to specify the address of a variable. They provide a convenient means of passing arguments to functions and for referring to more complex data types such as structures. You need to declare and initialize pointers just as you would other variables, but there are special operators that you need to use.


Address Operator (&): 

The "address of " operator (&) gives the memory address of the variable.


 Declaration and Initialization of Pointer variables:

type* pointer_name;

            //or

type *pointer_name;


type*pointer_name=&variable_name;








The * Operator Dereference: 

The star operator (*) dereferences a pointer. The * is a unary operator which goes to the left of the pointer it dereferences. The pointer must have a pointee, or it's a runtime error.




Pointer to Pointer:




Types of Pointers

i) Null Pointer

A pointer should be set to zero when it is not assigned to a valid address. Such a pointer is called a null pointer. Doing this will allow you to check whether the pointer can be safely dereferenced, because a valid pointer will never be zero.



i) Void Pointer 

A void pointer is a pointer that has no associated data type with it. A void pointer can hold address of any type and can be type casted to any type.




Incrementing Pointer:




Decrementing Pointer:



** Try the output of above programs by yourselfs...😀Hope this is going to be easy for you.