Share
Explore BrainMass

C++ Questions

1.
Consider

int a, b;

int *prt; //A pointer
int **prtPrt //A pointer to a pointer

ptr = &a;
prtPtr = &ptr;

a. Is this code legal?
b. What are the values of *ptr and **ptrPtr?
c. Using no other objects besides those already declared, how can you alter ptrPtr so that is points to a pointer to b without directly touching ptr?
d. Is the following statement legal?

2.
a. ls *&x always equal to x? If not, give an example
b. ls &*x always equal to x? Uf not, give an example

3.
For the declarations
int a = 5;
int *ptr - &a;

what are the values of the following?
a. ptr
b. *ptr
c. ptr == a
d. ptr == &a
e. &ptr
f. *a
g. *&a
h. **&ptr

4
Give the typr of each identifier declared here and the types of the expressions. Is any expression illegal?

a. struct X { inta; S *b; };
b. S z;
c. S *x;
d. x->a
e. x->b
f. z.b
g. z.a
h. *z.a
j. (*z).a
k. x->b-z.b
l. &z
m. &x

5
Is the following code legal? Why or why not?

int a = a;
const in & b = a;

6
What is worng with omitting a space in *x/*y?

Solution Preview

1. This should loook like this (after fixing syntax prob;lems in names):

int a, b;

int *ptr; //A pointer
int **ptrPtr; //A pointer to a pointer

ptr = &a;
ptrPtr = &ptr;

a. Is this code legal?
- Yes, this code is legal.

b. What are the values of *ptr and **ptrPtr?
The value of *ptr is equal to a. The value of **ptrPtr is also equal to a.

c. Using no other objects besides those already declared, how can you alter ptrPtr so that is points to a pointer to b without directly touching ptr?

*ptrPtr = &b ;

d. Is the following statement legal?
??? (nothing is shown)

2.
a. ls *&x always equal ...

Solution Summary

C++ questions are answered.

$2.19