35.7k views
4 votes
Which of the following statements are syntactically correct? This is the c++ language 1. val = p; 2. q = &val; 3. *p = val; 4. *p = *q; 5.

User M Barzel
by
8.2k points

1 Answer

4 votes

Final answer:

Assuming typical variable and pointer types in C++, statement 2 is syntactically correct if 'q' is a pointer to a pointer, statement 3 is correct if 'p' is a pointer, and statement 4 is correct if both 'p' and 'q' are pointers of the same type. Statement 1 is not correct without dereferencing 'p'.

Step-by-step explanation:

To determine if the statements are syntactically correct, we must know the types and declarations of val, p, and q. However, assuming val is a variable of a certain type (like int), p is a pointer to that type, and q is a pointer to a pointer to that type, we can proceed.

  • 1. val = p; - Incorrect if p is a pointer. It should be val = *p; to get the value pointed by p.
  • 2. q = &val; - Correct if q is a pointer to a pointer of val's type (e.g., int** q).
  • 3. *p = val; - Correct if p is a pointer to val's type.
  • 4. *p = *q; - Correct if p and q are pointers and q is a pointer to the same type as p.

Note: Without proper context such as type definitions and initial declarations, we cannot determine the absolute correctness of these statements. However, based on typical usage patterns in C++, the context provided allows for these conclusions.

User Mohit Chugh
by
7.7k points