What is pointer? Explain how the pointer variable declared and initialized.
1. Pointers are variables that are used to store the address of another variable.
2. Address of a variable is the memory location number which is allotted to the variable.
The memory addresses are 0, 1, 2, 3… and so on up to the capacity of the memory.
The address is normally displayed in hexadecimal form. Hexadecimal form is a representation of number somewhat similar to binary number. Here four binary digits are combined together to form a hexadecimal number.
3. Pointers unlike other variables do not store values. As stated they store the address of other variables.
4. It is already mentioned in the first statement that pointers are also variables. Hence, we can also have a pointer that is pointing to another pointer.
5. Syntax of pointer declaration : Data_type *ptr_name;
6. Wherein “Data_type” is the data type of the variable to which the pointer is supposed to point. If we want a pointer to point to an integer than, we need to have the data type of the pointer as “int”, for a float type data pointer should also be of the “float” type and so on.
6. The “ptr_name” is an identifier i.e. the name of the pointer. The same rules of identifiers apply to the pointer name as to any other variable declaration. The most important difference in the declaration of a pointer is the “*” sign given before the pointer name.
7. Hence, according to the syntax seen above, if we want to declare a pointer for “int” type data then we can declare it as given in the example below: int *p; Here, the pointer name is “p”. Hence, “p” can be used as a pointer to point to any of the variable of type “int”.
8. Syntax : data_type *var_name;
9. Example : int *p; char *p;