0% found this document useful (0 votes)
20 views13 pages

Pointers Q&A

A pointer is a variable that holds the memory address of another variable. Pointers allow for dynamic memory allocation and release of memory when objects are no longer needed. They can be declared using a data type followed by an asterisk, and initialized using the address operator (&) to point to another variable. Pointers are useful for structures like linked lists that refer to other objects of the same type through a self-referential pointer member.

Uploaded by

riscomputersir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views13 pages

Pointers Q&A

A pointer is a variable that holds the memory address of another variable. Pointers allow for dynamic memory allocation and release of memory when objects are no longer needed. They can be declared using a data type followed by an asterisk, and initialized using the address operator (&) to point to another variable. Pointers are useful for structures like linked lists that refer to other objects of the same type through a self-referential pointer member.

Uploaded by

riscomputersir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Pointers

One Mark Questions

Question 1.
What do you mean by pointer?
Answer:
A pointer is a variable that holds the memory address, usually the location of
another variable.

Question 2.
Mention any one advantage of pointer?
Answer:
The advantage of pointer is memory can be allocated or deallocated dynamically.

Question 3.
What is address operator?
Answer:
The symbol ‘&'(ampersand) is an address operator.

Question 4.
What is pointer operator?
Answer:
The symbol ‘*’ in a variable is a pointer operator.

Question 5.
How do declare pointer?
Answer:
Syntax: data-type * pointer-variable-name;
Example: int *intptr;

Question 6.
How to initialize pointer?
Answer:
Ex: int *intptr, a=10;
intptr = &a;

Question 7.
What is static memory?
Answer:
The fixed size of memory allocation and cannot be altered during runtime is called
static memory allocation.

Question 8.
What is dynamic memory?
Answer:
Allocation of memory at the time of execution (run time) is known as dynamic
memory allocation.

Question 9.
What is free store?
Answer:
The free store is a pool of unallocated memory heap given to a program for dynamic
memory allocation.

Question 10.
Write a definition for a variable of type pointer to float.
Answer:
Syntax: float *pointervariable;
Example: float *ptr;

Question 11.
What is new operator in C++?
Answer:
The new operator in C++, allocate memory dynamically.

Question 12.
What is a delete operator in C++?
Answer:
The delete operator in C++ releases dynamically allocated memory.

Question 1.
What do you mean by pointer? Explain with example.
Answer:
A pointer is a variable that holds the memory address, usually the location of
another variable. For example, variable ‘a’ memory is allocated at address 56000,
and pointer variable ‘ptr’ can hold the address of variable ‘a’ i.e., address 56000.

Question 2.
Mention any two advantages of pointer.
Answer:
The two advantages of the pointer are
• For objects memory can be allocated dynamically during runtime.
• When objects are not used, then memory can be released.
• Memory is efficiently used.

Question 3.
What is address operator? Give an example.
Answer:
The address operator is the ampersand symbol (&) and is the unary operator. It
returns the memory address of the operand,
int a, *ptr;
ptr = &a;

Question 4.
What is pointer operator? Give example.
Answer:
The pointer operator is an asterisk symbol (*) and is unary operator. It returns the
value located at the address of the operand.
For example,
int a, *ptr;
ptr = &a;
*ptr= 10.

Question 5.
How do declare pointer? Give an example.
Answer:
The pointer declaration syntax:

Datatype *pointer name;

Example: int *ptr;

Question 6.
How to initialize pointer? Give an example.
Answer:
The initialisation of pointer:

Pointervariable = &variable;

For example;

int a, *ptr;
ptr = &a;
From the above, address of variable ‘a’ is assigned to variable ‘ptr’.

Question 7.
What is static memory?
Answer:
The fixed size of memory allocation for data during program compilation is called
static memory allocation.

Question 8.
What is dynamic memory?
Answer:
The allocation of memory for data at the time of execution (run time) is known as
dynamic memory allocation.

Question 9.
What is a free store?
Answer:
The free store is a pool of unallocated memory heap given to a program for dynamic
memory allocation.

Question 10.
Illustrate the use of “self-referential structures” with the help of an example.
Answer:
A self-referential structure is used to create data structures like linked lists, stacks,
etc. Following is an example of this kind of structure where pointer variable *next is
of datatype ‘node’ which is the structure type itself:
struct node
{
int data;
node *next;
};

Question 11.
What is a new operator in C++?
Answer:
The new operator in C++, allocates memory dynamically.
For example,
pointer variable = new datatype;

Question 12.
What is delete operator in C++?
Answer:
The delete operator in C++ releases dynamically allocated memory.
Question 13.
What is array of pointer? Give an example.
Answer:
The one dimensional or two-dimensional pointer array is called array of pointer.
For example, int *ptr[5];
Where *ptr is array pointer variable and size of array is 5. i.e., ptr[0], ptr[l], ptr[2],
ptr[3], ptr[4].

Three Mark Questions

Question 1.
What are the advantages of a pointer?
Answer:
The advantages of the pointer are

• For objects memory can be allocated dynamically during runtime.


• When objects are no more required, then memory can be released.
• Memory is efficiently used because memory is allocated when needed and
freed when it is not needed for objects.

Question 2.
How dynamic memory allocation is different from static memory allocation?
Answer:
Allocation of memory for data is done at the time of execution (run time) is known
as dynamic memory allocation. Whereas allocation of memory is for data is done
during compilation in fixed size is called static memory allocation.

Question 3.
Illustrate the use of “self-referential structures” with the help of an example.
Answer:
A self-referential structure is used to create data structures like linked lists, stacks,
etc. Following is an example of this kind of structure where pointer variable *next is
of datatype ‘node’ which is the structure type itself:
struct node
{
int data;
node *next;
};

Question 4.
What is new operator in C++? Give an example.
Answer:
The new operator in C++ is used for dynamic storage allocation. This operator can
be used to create an object of any type.
General syntax of the new operator in C++:
The general syntax of a new operator in C++ is as follows:
pointer variable = new datatype;
In the above statement, new is a keyword and the pointer variable is a variable of
type datatype.

For example:
int *a=new int;
In the above example, the new operator allocates sufficient memory to hold the
object of datatype int and returns a pointer to its starting point. The pointer variable
holds the address of memory space allocated.

Question 5.
What is delete operator in C++? Give an example.
Answer:
The delete operator in C++ is used for releasing memory space when the object is no
longer needed.
General syntax of delete operator in C++:
delete pointer_variable;
For example, delete cptr;
In the above example, delete is a keyword and the pointer variable cptr is the
pointer that points to the objects already created in the new operator.

Question 6.
Show the general form of new and delete operator in C++?
Answer:
Example:
to understand the concept of new and delete memory management operator in C++:

#include <iostream.h>
void main()
{
//Allocates using new operator memory space in memory for storing an integer
datatype
int *a= new a;
*a=100;
cou<< “The Output is:a=”<<a;
//Memory Released using delete operator
delete a;
}
The output of the above program is
The Output is:a=100

In the above program, the statement:


int *a= new a;

Holds memory space in memory for storing an integer data type. The statement:
*a=100

This denotes that the value present in the address location pointed by the pointer
variable a is 100 and this value of a is printed in the output statement giving the
output shown in the example above. The memory allocated by the new operator for
storing the integer variable pointed by a is released using the delete operator as:
delete a;

Question 7.
What is array of pointers? Give an example.
Answer:
The one dimensional or two-dimensional pointer array is called array of pointer.
For example, int a=10,b=15,c=20,d=30,e=40, *ptr[5];
ptr[0] = &a;
ptr[l] = &b;
ptr[2] = &c;
ptr[3] = &d;
ptr[4] = &e;
Where *ptr is integer array pointer variable and size of array is 5. i.e.; ptr[0], ptr[l],
ptr[2], ptr[3], ptr[4], Each pointer can now contain the address of memory.

Question 8.
What is the relationship between array and pointers? Give an example.
Answer:
There is a relationship between arrays and pointers. This is explained with the
following example;
void main()
{
int a[5], i;
cout<< “Enter the 5 elements”;
for (i=0; i<5; i++)
cin >> *(a+i);
cout<< “The entered array elements are ” << endl;
for (i=0; i<5; i++)
cout>>*(a+i);
}
In the above program, ‘a’ is a integer array with five elements, and ‘i’ is integer
variable. During the compilation, only the first element address of the array is stored
i. e., a[0]. To access every other element of the array, it calculates the address by
adding ‘i’ units to the base address i.e., a[0]. The number of bytes in each unit of ‘i’ is
2 bytes.

The line cin >> *(a+i); can be interpreted as follows;


Consider the base address of a is 1000, then
Base address of a + 0 = 1000 + 0 = 1000 where i = 0 unit of 2 bytes
Base address of a + 1 = 1000 + 2 = 1002 where i = 1 unit of 2 bytes
Base address of a + 2 = 1000 + 4 = 1004 where i = 2 units of 2 bytes
Base address of a + 3 = 1000 + 6 = 1006 where i = 3 units of 2 bytes
Base address of a + 4 = 1000 + 8 = 1008 where i = 4 units of 2 bytes.

Question 9.
What is the relationship between string and pointers? Give example.
Answer:
There is a relationship between string and pointers. In C++ string means character
array. When a character array is declared then only its first element address is
stored. The rest of the elements can be accessed with the help pointer to character
array. This is explained with the following example;
The declaration char str[20]=”college”, *cptr;

Every increment of cptr and printing the value at the location of pointer gives the
next element of the character array.

Question 10.
What is the relationship between structures and pointers? Give example.
Answer:
The pointers can be used to access the member of structures by declaring pointer to
structure type. The following example clearly shows the relationship between
structures and pointers,
struct student
{
int rollno;
float fees;
};
student s1, *ptr; //declaration of structure ‘s1’ and pointer ‘ptr’.
ptr = &s1; // initialisation of pointer variable ptr makes it
pointer to structure
cin>> ptr->rollno; // accessing structure member ‘rollno’ through
pointer with ->for input
cin>>ptr->fees // accessing structure member ‘fees’ through pointer
with ->for input

Question 11.
What is the relationship between object and pointers? Give an example.
Answer:
The pointers pointing to objects are referred to as object .pointers. There is a
relationship between pointers and objects. Using pointers all the members of the
class can be accessed. The following example shows various operations that can be
performed on the members of the class through a pointer to objects.
Declaration:
class name *object_pointer, object;
Initialisation:
object_pointer = & object;
Invoking function call to member of the class: object_pointer->memberfunction();
For example;
class emp
{
private:
int empno;
char name[15];
float salary; –
public:
void readdata();
void printdata();
};
void main()
{
emp obj, *ptr;
ptr = &obj;
ptr-> readdata();
ptr->printdata();
}

Five Mark Questions


Question 1.
Show the general form of new and delete operators in C++?
Answer:
Example: To understand the concept of new and delete memory management
operator in C++:
#include <iostream.h>
void main()
{
//Allocates using new operator memory space in memory for storing an integer
datatype
int *a= new a;
*a=100;
cout<< ” The Output is:a=”<<a;
//Memory Released using delete operator
delete a;
}
The output of the above program is
The Output is:a=100
In the above program, the statement: int *a= new a;
Holds memory space in memory for storing an integer data type. The statement:
*a=100
This denotes that the value, present in the address location pointed by the pointer
variable a is 100 and this value of a is printed in the output statement giving the
output shown in the example above. The memory allocated by the new operator for
storing the integer variable pointed by a is released using the delete operator as:
delete a;

Question 2.
What is the relationship between object and pointers? Give an example.
Answer:
The pointers pointing to objects are referred to as object pointers. There is a
relationship between pointers and objects. Using pointers all the members of the
class can be accessed. The following example shows various operations that can be
performed on the members of the class through a pointer to objects.
Declaration : class name *object_pointer, object;
Initialisation object_pointer = & object;
Invoking function call to member of the class: object_pointer->memberfunction();
For example;
class emp
{
private:
int empno;
char name[15];
float salary;

public:

void readdata();
void printdata();
};
void main()
{
emp obj, *ptr;
ptr = &obj;
ptr-> readdata();
ptr->printdata();
}
In the above program, *ptr is a pointer to class ’emp’ and ‘obj’ is class ’emp’ type.
The pointer is initialised with the address of object ‘obj’ of class ’emp’ type. Then the
pointer ‘ptr’ is used to invoke function call to the member function ‘readdata()’ and
‘printdata()’using member access operator ->.

Question 3.
Explain with example by passing the reference.
Answer:
#include <iostream>
// function declaration
void swap(int& x, int& y);
int main ()
{
// local variable declaration:
int a = 100;
int b = 200;
cout<< “Before swap, value of a << a << endl;
cout << “Before swap, value of b << b << endl;
/* calling a function to swap the values.*/
swap(a, b);
cout<< “After swap, value of a << a << endl;
cout << “After swap, value of b << b << endl;
}
// function definition to swap the values.
void swap(int& x, int& y)
{
int temp;
temp = x; /* save the value at address x */
x = y; /* put y into x */
y = temp; /* put x into y */
return;
}
The output of the above program is:
Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :200
After swap, value of b :100

In C++, reference variables are possible and works like alias to original memory
locations. In the above program, the function void swap(int& x, int& y); is declared
with two arguments with x and y as reference variables. During the function call
swap() takes two arguments (formal arguments) ‘x’ and ‘y’ which are reference
variables to variables of actual arguments ‘a’ and ‘b’ respectively. The interchange of
value takes place between x and y in the body of swap() function. Here, variable ‘x’
and ‘y’ are used to manipulate the data values from the location ‘a’ and ‘b’ directly.

Question 4.
Explain with example by passing the pointers.
Answer:
#include <iostream>
// function declaration void swap(int *, int *);
int main ()
{
// local variable declaration:
int a = 100;
int b = 200;
cout << “Before swap, value of a :” << a << endl;
cout << “Before swap, value of b:”<< b << endl;
/* calling a function to swap the values.*/
swap(&a, &b);
cout << “After swap, value of a << a << endl;
cout <<“After swap, value of b << b << endl;
}
// function definition to swap the values, void swap(int *x, int *y)
{
int temp;
temp = *x; // assign to temp from the value at address x
*x = *y; // put value at address y into x
*y = temp; // put value of temp into value at the address y
}
The output of the above program is:
Before swap, value of a :100 Before swap, value of b :200
After swap, value of a :200
After swap, value of b :100

In the above program, swap() function is declared with two arguments of type
integer pointers. During the function call memory address of variable, ‘a’ and
variable ‘b’ is passed as actual arguments, and formal arguments are pointer ‘x’ and
pointer Y takes the address of ‘a’ and ‘b’ variables respectively. In the body of the
swap function, the pointer operator is used to access the value at the location. Here
the memory location of ‘a’ and ‘b’ are accessed by the pointers V and Y and produces
the output as shown above.

You might also like