What is Pointer? this is a question who trouble many of us, so i decide to write on it, hope it helps you 🙂
When we define a variable in c or whatever language, the compiler will find unused/vacant location in memory and allocate that to variable, to use. As we know area of memory consist of lot of cells and each cell is identified by a unique number, that unique number is known as memory address or address. Size of each memory cell is 1 byte.
Like any other data type as integer (int), character (char), floating point numbers (float), etc. Address also requires a data type to store, that is the reason pointer come in existence.
Declaration of pointer variable:
<Data Type> * <Variable name>; Example : int * p; // here p is pointer to integer or integer pointer similarly char * p1; // here p1 is pointer to character or character pointer
Finding address of a variable:
The address of variable can be obtained by using & (ampersand) operator, by placing the & operator in front of the variable’s name. when & operator is used as unary operator with a pointer variable, then it is known as address-of operator.
Example : int m; int *p; then p = &m;
here from the above statement, expression p = &m storing the address of m variable into the point variable p.
Important Points :
- The Address-of operator can be only used with variables and array elements.
- The data type of data variable and address variable must be same. let suppose p = &m here data types of pointer variable p and data variable m must be similar.
Getting Content of a variable using pointer :
To get content of a variable which is pointed by a pointer, uses the * (asterisk) operator. if * symbol used as unary operator with the pointer variable then it gives value of that variable. Then * operator is known as de-referencing operator.
example : int x = 1, y; int *p = &x; // intializing pointer variale p with address of x y = *p; //getting content of that address to which it is pointing i.e. x printf("%d", &y); // here it gives output as 1.
If You need help on any specific topic. Please let me know. I will try best to solve your problem. Thanks, 🙂