Reputation: 4595
I know this could be a dumb question.
I am totally confused about this, i accept i have not understood the basics properly. Why did
BOOL *booleanTest = (5 < 1)? YES : NO;
did not throw a compilation error, it is a primitive datatype and it cannot have pointer, what made it to compile and return yes always, irrespective of condition inside.
Please bless me with the reason and also why
int *magicNumber = value / 25;
did not throw a compilation error.
Upvotes: 0
Views: 232
Reputation: 62
In both the cases you are declaring the variables and using them there itself without any sort of initialization.
I am assuming that you do not know the difference between declaration and initialization so during declaration the value of the variables is set to garbage or some random value. How that is assigned is because the memory cell that the pointer is pointing to is some random memory cell which was used by some other application which has left the value there. Now what happens in the first case:
BOOL *booleanTest = (5 < 1)? YES : NO;
The pointer booleanTest is set to type BOOL and the value that it is pointing is only half a number that is required to point to a full memory cell. So it takes up the first few bits of the cell and which is probably 1 so it is coming to YES always.
In the second case
int *magicNumber = value / 25;
This won't cause any compilation error as it is legally allowed to store values inside a pointed value. It gets type casted to the exact variable type of the pointer.
Upvotes: -1
Reputation: 237080
C is not as strictly typed as you apparently believe. Assigning an integer to a pointer is legal, though usually unwise. The compiler should have warned you that the assignment makes a pointer from an integer without a cast, though.
Upvotes: 2