Pointer
is a variable that points to or references a memory location where data is
stored. Each memory cell in the computer has an address which can be used to
access its location. A pointer variable points to a memory location rather than
a value. It has two parts: the pointer itself holds the address and the address
point to a variable.
Some
of the uses/advantages of pointer are
1. Pointers
are more efficient in handling array and data tables.
2. Pointers
are necessary for dynamic memory location.
3. They
can be used to return multiple values from a function via function arguments.
4. Implementing
data structure like linked lists, trees, graphs, queues, stacks.
5. Pointers
reduce length and complexity of programs thus increases the execution speed and
reduces the program execution time.
6. Use
of pointer array to character string results in saving data storage in memory.
The
pointer declaration syntax:
pointer_type *pointer_variable_name;
For
example: int *p;
Address
(&) and Indirection (*) Operator
The
& operator and immediately preceding variable returns the address of the
variable associate with it. Hence, the address of (&) operator returns the
address of memory location in which the variable is stored.
The
indirection(*) operator is used to extract value from the memory location
stored in the particular memory location whose address is stored in pointer
variable
pointer_variable = &variable_name;
For
example:
int * ptr, num=25;
ptr =#
Places
the memory address of num variable into the pointer variable ptr
If
we want to find out the value stored in the ptr variable then,
*(ptr);
or *(&num);
Explain
the meaning of each of the following declaration
1) int
*p : - pointer to an integer
2)
int
*p[10]:-pointer to an array of 10 integer.
3)
int(*p)[10]:-
*p is an array of size 10;
4)
int
*p(void) :- function doesn't pass any value to the pointer function p since it
has void arguments
5)
int *p(char*a):-this function pass
the memory address if the character variable to pointer variable p since it has
* a arguments.
Difference
between Array and Pointer
Array
|
Pointer
|
Array is a
static memory allocation.
Once memory is allocated, it cannot be resized or freed dynamically.
|
Pointer is a
dynamic memory allocation.
The memory allocation can be resized or freed later.
|
Array cannot
access the value of pointer
|
Pointer can
access the value of the pointer.
|
Execution speed
of the program is slow in comparison to pointer.
|
Execution speed
of the program is very fast.
|
Array refer data
value in memory location
|
Pointer refers
address to the memory location.
|
Array can be initialized at definition. Example int num[ ] = { 1,2, 3} |
Pointer can’t be initialized at
definition.
|
Similarities
between Array and Pointer
1. Two pointer
variables cannot be added and pointer variable cannot be multiply by a constant
similarly two array variables cannot be added and array variable cannot be
multiply by a constant.
2. Pointer can holds
the memory addresses of multiple variables of same data type and array can hold
the value of similar data type.
No comments:
Post a Comment