Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Pointer

1. What is a pointer? Write the importance / advantages / benefits of using pointer.


Ans:

Pointer:
A pointer is a variable that represents the location (rather than the value) of a data item, such as a
variable or an array element.

Benefits of using pointer:

1. Pointers are more efficient in handling arrays and data tables.


2. Pointers can be used to return multiple values from a function via function arguments.
3. Pointers permit references to functions and thereby facilitating passing of functions as
arguments to other functions.
4. The use of pointer array to character strings results in saving of data storage space in
memory.
5. Pointers allow C to support dynamic memory management.
6. Pointers provide an efficient tool for manipulating dynamic data structures such as
structures, linked lists, queues, stacks and trees.
7. Pointers reduce length and complexity of programs.
8. They increase the execution speed and thus reduce the program execution time.

2. What is scale factor?


Ans:
If p1 is an integer pointer with an initial value, say 2800, then after the operation p1=p1+1, the
value of p1 will be 2802, and not 2801. That is, when we increment a pointer, its value is
increased by the length of the data type that it points to. This length called the scale factor.

[Additional:] For an IBM pc, the lengths of various data types are as follows:

characters 1 byte
integers 2 bytes
floats 4 bytes
long integers 4 bytes
doubles 8 bytes

Dr. Abu Nowshed Chy 1


Lecturer, Dept. CSE, CU
3. Explain: Pointer can create a perfect copy of variables.
Ans:

Whenever we declare a variable, the system allocates, somewhere in the memory, an appropriate
location to hold the value of the variable. Consider the following statement:
int quantity = 179;
This statement instructs the system to find a location for the integer variable quantity and put the
value 179 in that location. We may represent this below figure:

We may have access to the value 179 by using either the name quantity or the address 5000.
Since memory addresses are simply numbers, they can be assigned to some variables too. Such
variables are called pointer variable. A pointer variable is, therefore, nothing but a variable that
contains an address, which is the location of another variable in memory.

Since the pointer is a variable, its value is also stored in the memory in another location.
Suppose, we assign the address of quantity to a variable p. The link between the variables
p and quantity can be visualized as follows:

Since the value of the variable p is the address of the variable quantity, we may access the value
of quantity by using the value of p and therefore, we can say that pointer can create a
perfect copy of variables.

Dr. Abu Nowshed Chy 2


Lecturer, Dept. CSE, CU
4. How a pointer is declared and initialized?
Or
Explain the initialization of the pointer variable.
Ans:

A pointer declaration may be written in general terms as

data-type *ptvar;

where ptvar is the name of the pointer variable, and data-type refers to the data type of
the pointer’s object. An asterisk (*) must precede ptvar which denotes that the variable
ptvar is a pointer variable.

The process of assigning the address of a variable to a pointer variable is known as initialization.
Once a pointer variable is declared, we can use the assignment operator to initialize the variable.

For example, consider a variable,


int quantity;

Then we can declare and initialize a pointer variable as follows:


int *p; /* declaration */
p = &quantity; /* initialization */

In summary, initialization of Pointer can be done using the following 4 Steps :


1. Declare a Pointer Variable and Note down the Data Type.
2. Declare another Variable with the Same Data Type as that of Pointer Variable.
3. Initialize Ordinary Variable and assign some value to it.
4. Now Initialize pointer by assigning the address of the ordinary variable to the pointer
variable.

Example:
#include<stdio.h>
int main()
{
int a; // Step 1
int *ptr; // Step 2
a = 10; // Step 3
ptr = &a; // Step 4

return(0);
}

Dr. Abu Nowshed Chy 3


Lecturer, Dept. CSE, CU
5. What do you mean by chain of pointers?
Ans:
Chain of pointers:
It is possible to make a pointer to point to another pointer, thus creating a chain of pointers as
shown.

P2 p1 variable
address 1 address 2 value

Here, the pointer variable p2 contains the address of the pointer variable p1, which points to the
location that contains the desired value. This is known as multiple indirections.

A variable that is a pointer to a pointer must be declared using additional indirection operator
symbols in front of the name. Example:
int **p2;

6. Distinguish between (*m)[5] and *m[5].


Ans:

Since * has a lower precedence than [ ] , *m[5] declares m as an array of 5 pointers while
(*m)[5] declares m as a pointer to an array of five elements.

7. Using example define pointer of array and array of pointer?


Or
What is The Significance of pointer in application of array.

Ans:

An array is a series of elements of the same data type. Pointers can be used to manipulate arrays
rather than using an index. The name of an array points to the first element of the array. If you
declare an array in the following manner:
int marks[5];

Pointer of array:
You can point to the first element of the array using either one of the following pointers:
marks //first element
&marks[0] //first element

you can use the array name to refer to the contents of the array elements like:
*(marks) //value of 1st element
*(marks+1) //value of 2nd element

Dr. Abu Nowshed Chy 4


Lecturer, Dept. CSE, CU
Array of pointer:
An array of pointers can be defined as:
data-type *array[expression];
For example,
char *name[3] = {
“New Zealand”,
“Australia”,
“India”
};
declares name to be an array of three pointers to characters, each pointer pointing to a particular
name as:
name[0]  New Zealand
name[1]  Australia
name[2]  India

8. What do you know about call by reference and call by value.


Or
When to Use Call by Value and When to use Call by Reference?
Ans:
Call by value:
In call by value, a copy of actual arguments is passed to formal arguments of the called function
and any change made to the formal arguments in the called function have no effect on the values
of actual arguments in the calling function.

Call by reference:
In call by reference, the location (address) of actual arguments is passed to formal arguments of
the called function. This means by accessing the addresses of actual arguments we can alter them
within from the called function.

When to Use Call by Value and When to use Call by Reference?


One advantage of the call by reference method is that it is using pointers, so there is no doubling
of the memory used by the variables (as with the copy of the call by value method). This is of
course great, lowering the memory footprint is always a good thing. So why don’t we just make
all the parameters call by reference?

There are two reasons why this is not a good idea and that a programmer need to choose between
call by value and call by reference. The reasons are: side effects and privacy. Unwanted side
effects are usually caused by inadvertently changes that are made to a call by reference
parameter. Also in most cases, you want the data to be private and that someone calling a
function only be able to change if you want it. So it is better to use a call by value by default and
only use call by reference if data changes are expected.

Dr. Abu Nowshed Chy 5


Lecturer, Dept. CSE, CU

You might also like