Showing posts with label CPP Pointers. Show all posts
Showing posts with label CPP Pointers. Show all posts

Tuesday, March 15, 2016

Write a code fragment that dynamically allocates a structure of the type described in Question 8 and then reads a value for the kind member of the structure.

Leave a Comment

Question:

Write a code fragment that dynamically allocates a structure of the type described in Question 8 and then reads a value for the kind member of the structure.

Answer:

struct fish
{
char kind[20];
int weight;
float length;
};

fish * pole = new fish;
cout << "Enter kind of fish: ";
cin >> pole->kind;

Source:

C++ Primer Plus Sixth Edition Chapter Review

Read More

Is the following valid code? If so, what does it print? cout << (int *) “Home of the jolly bytes”;

Leave a Comment

Question:

 Is the following valid code? If so, what does it print?              

cout << (int *) “Home of the jolly bytes”;

Answer:

Ye s , it is valid.The expression "Home of the jolly bytes" is a string constant; hence it evaluates as the address of the beginning of the string.The cout object interprets the address of a char as an invitation to print a string, but the type cast (int *) converts the address to type pointer-to-int, which is then printed as an address. In short, the statement prints the address of the string, assuming the int
type is wide enough to hold an address.

Source:

C++ Primer Plus Sixth Edition Chapter Review

Read More

Suppose ted is a double variable. Declare a pointer that points to ted and use the pointer to display ted’s value.

Leave a Comment

Question:

Suppose ted is a double variable. Declare a pointer that points to ted and use the pointer to display ted’s  value.

Answer:

double * pd = &ted;
cout << *pd << "\n";

Source:

C++ Primer Plus Sixth Edition Chapter Review

Read More