a pointer is variable which keeps addresses in memory .
other data values, memory addresses, or pointer values, can be stored
in variables of the appropriate type. A variable that stores an
address is called a pointer variable, but is often simply referred to
as just a pointer. The definition of a pointer variable, ptr, must
specify the type of data that ptr will point to. Here is an example:
asterisk before the variable name indicates that ptr is a pointer
variable, and the int data type indicates that ptr can only be used
to point to, or hold addresses of, integer variables. This definition
is read as “ptr is a pointer to int.” it is also useful to think
of *ptr as the “variable that ptr point to.” with this view, the
definition of ptr just given can be read as “as the variable that
ptr points to has type int.’ because the asterisk (*) allows you to
pass from a pointer to the variable being pointed to, it is called
the indirection operator.
It is the
responsibility of the programmer to keep track of what type of data
is stored in each memory location. The data might be a number or some
text (which is just a sequence of numbers, of course) or it might be
an address of another location or possible an address of an address
and so forth. There are also some high level languages – un typed
languages – that operate in the same way; Forth and BCPL are examples
that come to mind.
majority of high level languages support data typing to a lesser or
greater extent. This means, in effect, that the programmer specifies
that a variable contains a specific type of data and the language
only allows appropriate operations on that variable. Advantage of
using pointers is efficient in handling Arrays and Structures,
Pointers allow references to function and it helps in passing of
function as arguments to other functions, it reduces length of the
program and its execution time as well and it allows C language to
support Dynamic Memory management.
IS THE PURPOSE OF DEVELOPER POINTERS
learned earlier in before chapter that an array name, without
brackets and a subscript, actually represents the starting address of
the array. This means that an array name is pointer. Program next
page by showing an array name being used with the indirection
array elements are stored together in memory, as illustrated in
make sense that if numbers is the address of numbers 0, values
could be added to numbers to get the addresses of the other elements
in the array. In other words, if you add one to number, you are
actually adding 1 * sizeof (short) to numbers. If you add two to
numbers , the result is numbers +2* sizeof (short), and so on. This
conversion means that an element in an array can be retrieved by
adding its subscript to a pointer to the array.
are used to keep the address of other variables of similar data type.
But if you want to store the address of a pointer variable, then you
again need to store it. Thus, when one pointer variable stores the
address of another pointer variable, it is known as Pointer to
Pointer variable or Double Pointer. Here, we have used two
indirection operator(*) which stores and points to the address of a
pointer variable i.e, int *. If we want to store the address of this
(double pointer) variable p1, then the syntax will become. For
also have array of structure variables. And to use the array of
structure variables readily, we use pointers of structure type.. To
access members of structure using the structure variable, we used the
dot . Operator. But when we have a pointer of structure type, we use
arrow -> to access structure members.
pointer variables changed with mathematical statements that perform
addition or subtraction. This is demonstrated in program next page.
The first loop increments the pointer variable, stepping it through
each element of the array backwards. Not all arithmetic operations
may be performed on pointers. For example, you cannot use
multiplication or divison with pointers the following operations are
allowable: the ++ and – operators may
be used to increment or decrement a pointer variable. An integer may
be added to or substrated from a pointer variable. This may be
performed with the + and- operators. And a pointer may be substracted
from another pointer. a few examples and understand this more
clearly. int*i; i++; increment it, it will increment by 2 bytes
because int is also of 2 bytes. float*i; i++; increment it, it will
increment by 4 bytes because float datatype is of 4 bytes. double*i;
i++; Similarly, in this case, size of pointer is still 2 bytes. But
now, when we increment it, it will increment by 8 bytes because its
data type is double.
as a function parameter is used to hold addresses of arguments passed
during function call. This is also known as call by reference. When a
function is called by reference any change made to the reference
variable will effect the original variable. It is possible to declare
a pointer pointing to a function which can then be used as an
argument in another function. A pointer to a function is declared as
follows, type (*pointer-name)(parameter);. Here is an example: int
(*sum)(); int *sum();A function pointer can point to a specific
function when it is assigned the name of that function. Int sum (int,
int); int (*s)(int, int); s=sum; Here
s is a pointer to a function sum. Now sum can be called using
function pointer s along with providing the required argument values.
it, it will increment by 2 bytes because int is also of 2 bytes.
pointer to integer:
It is commonly to
store the value of a pointer (i.e. an address) in an “ordinary”
variable – like an unsigned integer. An example of where this might
be done in fixed is in device. Here is an example:
pointer = ;
This would result
in the variable normal containing its own address.