Here we are discuss how pointer works on different types? Pointer is a vital topic in C programming language. Every programmer should follow the basic rules of pointer.
What is a pointer?
The pointer is a variable that stores or points address of another variable. The pointer is a variable that might be belonging to any of the data types means int, float, char, long etc.
Syntax Of Pointer:
data-type *variable-name;
Example:
int *p; char *p;
Here “*” denotes that p is a pointer variable not a normal variable.
Example:
#include<stdio.h> int main() { int a=2,*p; float b=3, *q; char c='a', *r; p = &a; q = &b; r = &c; printf("a=%d\t,*p=%d\n",a,*p); printf("b=%f\t,*q=%f\n",b,*q); printf("c=%c\t,*r=%c\n",c,*r); return 0; }
#Output a=2 *p=2 b=3.00000 *q=3.00000 c=a *r=a
A pointer in C programming is called a Dynamic Memory Allocation means to allocate memory at runtime.
Some Key Points Remember In Pointer C Programming
- Normal Variables are stores the value whereas pointer variables are always stores address.
- The content of the pointer variable is always a whole number i.e. address.
- Always pointer variables are initialized with null, means *p = null.
- The value of a null pointer is 0.
- “&” symbol is used the reference variable in “C” Programming.
- “*” symbol is used to refer the value of pointer variable that pointer points.
- If a pointer assigning a “null” means points to nothing.
- Two pointers can be subtracted to know how many elements are remaining between these 2 pointers.
- But pointer addition, multiplication and divisions are not allowed.
- The size of the pointer depends on the type of variable, that pointer points. Means see an Example:
#include<stdio.h> int main() { int a=2,*p; p=&a; printf("a=%d\t,*p=%d",a,*p); return 0; }
#Output a=2 *p=2
Remember:
The pointer always declare the same type the variable declare.
If you find any issue and interested to rewrite it then contact us.