C++ Pointers and Functions

  Home  C++ Programming  C++ Pointers and Functions


“C++ Pointers & Functions frequently Asked Questions by expert members with experience in C++ pointers and functions. These questions and answers will help you strengthen your technical skills, prepare for the new job test and quickly revise the concepts”



32 C++ Pointers And Functions Questions And Answers

1⟩ Explain what is NULL pointer and void pointer and what is their use?

A NULL pointer has a fixed reserved value that is not zero or space, which indicates that no object is referred. NULL pointers are used in C and C++ as compile-time constant. NULL pointer represents certain conditions, like successor to the last element in linked list, while a consistent structure of the list of nodes are maintained.

A void pointer is a special pointer that points to an unspecified object. A null pointer can not be dereferenced. The address manipulation can directly be done by pointer casting to and from an integral type of sufficient size.

 244 views

2⟩ What is void pointer using C++?

In C++, void represents the absence of type, so void pointers are pointers that point to a value that has no type. The void pointers can point to any data type.

We can declare void pointer as follows.

Void *p;.

 252 views

3⟩ What is const reference?

const references allow you to specify that the data referred to won't be changed. A const reference is actually a reference to const. A reference is inherently const, so when we say const reference, it is not a reference that can not be changed, rather it’s a reference to const. Once a reference is bound to refer to an object, it can not be bound to refer to another object. For example:

int &ri = i;

binds ri to refer to i. Then assignment such as:

ri = j;

doesn’t bind ri to j. It assigns the value in j to the object referenced by ri, ie i;

This means, if we pass arguments to a function by const references; the function can not change the value stored in those references. This allows us to use const references as a simple and immediate way of improving performance for any function that currently takes objects by value without having to worry that your function might modify the data. The compiler will throw an error if the function tries to modify the value of a const reference.

 248 views

4⟩ Can you please explain the difference between an inspector and a mutator?

An object’s state is returned without modifying the object’s abstract state by using a function called inspector. Invoking an inspector does not cause any noticeable change in the object’s behavior of any of the functions of that object.

A mutator, on the other hand, changes the state of an object which is noticeable by outsiders. It means, it changes the abstract state of the object.

The following code snippet depicts the usage of these two functions:

class ShoppingCart {

public:

int addItem(); //Mutator

int numItems() const; //Inspector

};

The function addItems() is a mutator. The reason is that it changes the ‘ShoppingCart’ by adding an item.

The function numItems() is an inspector. The reason is that it just updates the count of number of items in the ‘ShoppingCart’. The const declaration followed by int numItems() specifies that numItems() never change the ‘ShoppingCart’ object..

 221 views

5⟩ When we use this pointer?

'this pointer' is used as a pointer to the class object instance by the member function. The address of the class instance is passed as an implicit parameter to the member functions.

 232 views

6⟩ What is const pointer?

const pointer is a pointer which you don’t want to be pointed to a different value. That is, the location stored in the pointer can not change. We can not change where the pointer points. It is declared as:

type * const name

type is data type

name is name of the pointer

eg: char * const p

Since the location to which a const pointer points to can not be changed, the following code:

char ch1 = ‘A’;

char ch2 = ‘B’;

char * const p = &ch1;

p = &ch2;

will throw an error since address stored in p can not be changed.

 238 views

7⟩ Can you please explain the use of this pointer?

When a member function is called, it is automatically passed an implicit argument that is a pointer to the invoking object (ie the object on which the function is invoked). This pointer is known as this pointer. It is internally created at the time of function call.

The this pointer is very important when operators are overloaded.

Example: Consider a class with int and float data members and overloaded Pre-increment operator ++:

class MyClass

{

int i;

float f;

public:

MyClass ()

{

i = 0;

f = 0.0;

}

MyClass (int x, float y)

{

i = x; f = y;

}

MyClass operator ++()

{

i = i + 1;

f = f + 1.0;

return *this; //this pointer which points to the caller object

}

MyClass show()

{

cout<<”The elements are:n” cout<i<<”n<f; //accessing

data members using this

}

};

int main()

{

MyClass a;

++a; //The overloaded operator function ++()will return a’s this

pointer

a.show();

retun 0;

}

The o/p would be:

The elements are:

1

1.0

 233 views

9⟩ What is pointer to member?

not to a specific instance of that member in an object. This type of pointer is called a pointer to a class member or a pointer-to-member. It is not same as normal C++ pointer. Instead it provides only an offset into an object of the member’s class at which that member can be found. Since member pointers are not true pointers, the . and -> can not be applied to them. Instead we must use the special operators .* and ->* . They allow access to a member of a class.

Example:

#include <iostream>

using namespace std;

class MyClass

{

public:

int val;

MyClass(int i)

{

val = i;

}

int double_val()

{

return val + val;

}

};

int main()

{

int MyClass::*data; //data member pointer

int(MyClass::*func)(); //function member pointer

MyClass obj1(1), obj2(2); //create objects

data =&MyClass::val; //get offset of data val

func=&MyClass::double_val; //get offset of function double_val()

cout << “The values are:”;

cout << ob1.*data << “ “ <<ob2.*data << “n”;

cout<< “Here they are doubled:”;

cout << (ob1.*func)() << “ “ <<(ob2.*func)()<< “n”;

return 0;

}

Here data and func are member pointers. As shown in the program, when declaring pointers to members, you must specify the class and use the scope resolution operator.

 230 views

10⟩ What is smart pointer?

Smart pointers are objects which store pointers to dynamically allocated (heap) objects. They are like built-in C++ pointers. However, they automatically delete the object pointed to at the appropriate time. They are useful as they ensure proper destruction of dynamically allocated objects (Exceptions). They can also be used to keep track of dynamically allocated objects shared by multiple owners.

They appear as owning the object pointed to and are responsible for deletion of the object when it is no longer needed.

The smart pointer library provides five smart pointer class templates:

scoped_ptr : Simple sole ownership of single objects. Noncopyable.

scoped_array: Simple sole ownership of arrays. Noncopyable.

shared_ptr: Object ownership shared among multiple pointers

shared_array: Array ownership shared among multiple pointers.

weak_ptr: Non-owning observers of an object owned by shared_ptr.

intrusive_ptr: Shared ownership of objects with an embedded reference count.

These templates are designed to complement the std::auto_ptr template.

 220 views

11⟩ What is Pointer to constant?

Pointer to constant points to a value that does not change and is declared as:

const type * name

type is data type

name is name of the pointer

e.g: const char *p;

pointer to constant can not be used to change the value being pointed to. Therefore:

char ch = ‘A’;

const char *p = &ch;

*p = ‘B’;

is not allowed. The program will throw an error.

 237 views

12⟩ What is Pointer Constant?

Pointer Constant (or constant pointer) is a pointer which you don’t want to be pointed to a different value. That is, the location stored in the pointer can not change. We can not change where the pointer points. It is declared as:

type * const name

type is data type

name is name of the pointer

eg: char * const p

Since the location to which a const pointer points to can not be changed, the following code:

char ch1 = ‘A’;

char ch2 = ‘B’;

char * const p = &ch1;

p = &ch2;

will throw an error since address stored in p can not be changed.

 240 views

13⟩ Explain pointer with examples?

A pointer is a variable that holds a memory address. This address is the location of another object (typically, a variable) in memory. That is, if one variable contains the address of another variable, the first variable is said to point to the second.

A pointer declaration consists of a base type, an *, and the variable name. The general form of declaring a pointer variable is:

type *name;

type is the base type of the pointer and may be any valid type.

name is the name of pointer variable.

The base type of the pointer defines what type of variables the pointer can point to.

 232 views

14⟩ Explain function pointers?

A function has a physical location in the memory which is the entry point of the function. And this is the address used when a function is called. This address can be assigned to a pointer. Once a pointer points to a function, the function can be called through that pointer. Function pointers also allow functions to be passed as arguments to other functions. The address of a function is obtained by using the function’s name without any parenthesis or arguments.

Consider following example:

#include <iostream>

using namespace std;

void check(char *a, char *b, int (*cmp) (const char*, const*));

int numcmp(const char *a, const char *b);

int main()

{

char s1[80], s2[80];

gets (s1);

gets (s2);

if (isalpha(*s1))

check(s1, s2, strcmp);

else

check(s1, s2, numcmp);

return 0;

}

void check(char *a, char *b, int (*cmp) (const char*, const*))

{

cout <<”Testing for equality n”;

if(!(*cmp)(a,b))

cout <<“Equal”;

else

cout <<“ Not Equal”;

}

int numcmp(const char *a, const char *b)

{

if(atoi(a) == atoi(b))

return 0;

else

return 1;

}

In this function, if you enter a letter, strcmp() is passed to check() otherwise numcmp() is used.

 227 views

15⟩ Explain const pointer and const reference?

const pointer is a pointer which you don’t want to be pointed to a different value. That is, the location stored in the pointer can not change. We can not change where the pointer points. It is declared as:

type * const name

type is data type

name is name of the pointer

eg: char * const p

Since the location to which a const pointer points to can not be changed, the following code:

char ch1 = ‘A’;

char ch2 = ‘B’;

char * const p = &ch1;

p = &ch2;

will throw an error since address stored in p can not be changed.

const reference:

const references allow you to specify that the data referred to won't be changed. A const reference is actually a reference to const. A reference is inherently const, so when we say const reference, it is not a reference that can not be changed, rather it’s a reference to const. Once a reference is bound to refer to an object, it can not be bound to refer to another object. For example:

int &ri = i;

binds ri to refer to i. Then assignment such as:

ri = j;

doesn’t bind ri to j. It assigns the value in j to the object referenced by ri, ie i;

This means, if we pass arguments to a function by const references; the function can not change the value stored in those references. This allows us to use const references as a simple and immediate way of improving performance for any function that currently takes objects by value without having to worry that your function might modify the data. The compiler will throw an error if the function tries to modify the value of a const reference.

 225 views

16⟩ Can you please explain the difference between Pointer to constant and pointer constant?

Pointer to constant points to a value that does not change and is declared as:

const type * name

type is data type

name is name of the pointer

e.g: const char *p;

pointer to constant can not be used to change the value being pointed to. Therefore:

char ch = ‘A’;

const char *p = &ch;

*p = ‘B’;

is not allowed. The program will throw an error.

Pointer Constant (or constant pointer) is a pointer which you don’t want to be pointed to a different value. That is, the location stored in the pointer can not change. We can not change where the pointer points. It is declared as:

type * const name

type is data type

name is name of the pointer

eg: char * const p

Since the location to which a const pointer points to can not be changed, the following code:

char ch1 = ‘A’;

char ch2 = ‘B’;

char * const p = &ch1;

p = &ch2;

will throw an error since address stored in p can not be changed.

 240 views