Page 98 - 6437
P. 98
Pointers
Pointers in C are easy and fun to learn. Some C programming tasks are performed more
easily with pointers, and other tasks, such as dynamic memory allocation, cannot be performed
without using pointers. So it becomes necessary to learn pointers to become a perfect C
programmer. Let's start learning them in simple and easy steps.
As you know, every variable is a memory location and every memory location has its
address defined which can be accessed using ampersand (&) operator, which denotes an address
in memory. Consider the following example, which prints the address of the variables defined:
When the above code is compiled and executed, it produces the following result:
Address of var1 variable: bff5a400
#include <stdio.h>
Address of var2 variable: bff5a3f6
int main ()
{
int var1;
char var2[10];
printf("Address of var1 variable: %x\n", &var1 );
printf("Address of var2 variable: %x\n", &var2 );
return 0;
}
What are Pointers?
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. Take a look at some of the valid pointer declarations:
The actual data type of the value of all pointers, whether integer, float, character, or
int *ip; /* pointer to an integer */
double *dp; /* pointer to a double */
float *fp; /* pointer to a float */
char *ch /* pointer to a character */
10
1