Member-only story
C++ Pointers Practice Questions
1. What is a pointer?
A pointer is a variable that stores the memory address of an object. Pointers are used extensively in both C and C++ for three main purposes: to allocate new objects on the heap, to pass functions to other functions. to iterate over elements in arrays or other data structures.
2. Do pointers have a data type?
Yes. Like regular variables, pointers have a data type. For example, a pointer of type integer can hold the address of a variable of type integer. A pointer of character type can hold the address of a variable of character type.
3. What symbol is used to know the location in the computer memory where the data is stored?
C++ provides the & (reference) operator. The operator returns the address that a variable occupies.
For example, if x is a variable, &x returns the address of the variable.
4. How do you declare a pointer?
datatype *variable_name;
- The datatype is the base type of the pointer which must be a valid C++ data type.
- The variable_name is should be the name of the pointer variable.
- Asterisk used above for pointer declaration is similar to asterisk used to perform multiplication operation. It is the asterisk that marks the variable as a pointer.