Exercise Sheet 4, Task 9

In the exercise, we have the following cases:

char* d;
d = "Types are fun";
d = (1==2);
char* y;
y = 1;

In the sample solution, the assignment d = (1==2) is marked as invalid, while y = 1 is marked as valid. Why is that?

Exercise 9.: C Types

You are right, both expressions are invalid ^{(1)} because the right side is an int and the left side a char *.
Such assignments need an explicit cast.

Exception ^{(1)}: You can assign the null pointer constant to a pointer.
Some compilers will accept d=(1==2) because 1==2 is false which is 0 which can be assigned as null pointer.

1 Like