Pointers In C
Pointers In C
Pointers In C
What is pointer?
A pointer is a variable that points at, or refers to, another variable.
A pointer is a variable whose value is the address of another variable,
i.e., direct address of the memory location. Like any variable or constant,
you must declare a pointer before using it to store any variable address–
The general form of a pointer variable declaration is - type *var-name;
Here, type is the pointer's base type; it must be a valid C data type
and var-name is the name of the pointer variable. The asterisk *
used to declare a pointer is the same asterisk used for
multiplication. However, in this statement the asterisk is being
used to designate a variable as a pointer.
Example-
int *ip; /* pointer to an integer */
double *dp; /* pointer to a double */
float *fp; /* pointer to a float */
char *ch /* pointer to a character */
Pointer Notation
Consider the example given below
int i = 3;
We see that the computer has selected memory location 65524 as the
place to store the value 3. The location number 65524 is not a number
to be relied upon, because some other time the computer may choose a
different location for storing the value 3. The important point is, i’s
address in memory is a number.
We can take the above output using the program given below
Points to remember
(a)Pointers are variables which hold addresses of other variables.
(b) A pointer to a pointer is a variable that holds address of a pointer variable.
(c) The & operator fetches the address of the variable in memory.
(d) The * operator lets us access the value present at an address in memory
with an intension of reading it or modifying it.
(e) A function can be called either by value or by reference.
(f) Pointers can be used to make a function return more than one value
simultaneously in an indirect manner.