5 ms·
To be fair, this is an absolutely essential thing to be a stickler about in C. It's a dangerous language, and placement of semicolons and recognition of unitit
by nitrogen 10y ago
To be fair, this is an absolutely essential thing to be a stickler about in C. It's a dangerous language, and placement of semicolons and recognition of unititialized variables should be burned deep into the psyche of an experienced C developer.
This is why people advocate for alternatives like Rust where the compiler can be precise on your behalf. But as long as C is still in use, its practitioners need to be exceptionally pedantic with their code.
- gengkev 10y agoThat is true, but my point is that in the context of an interview, the purpose of "char *p;" might simply be to indicate the type of p, but not necessarily to indicate that it is uninitialized. It's hard to know what the interviewee thought without asking them. Though it might be a good thing to say "well, p is uninitialized," it might also come across as obnoxious if that wasn't what the interviewer intended. So it comes down to guessing which response the interviewer is looking for.
- eveningcoffee 10y agoI do not know how to convince you as others have failed, but I really believe that trying to understand the correct understanding about the memory allocation was the essence of these questions. The final answer that the memory was dynamically allocated by the compiler is as wrong as it can be. If I saw this code then my first reaction was that this code probably will produce a segmentation fault in the best case scenario. Well, that was after I verified that this is not some pointer usage question, as I am not an experienced C developer.
- adekok 10y ago> the purpose of "char * p;" might simply be to indicate the type of p, but not necessarily to indicate that it is uninitialized. It's C code. Your interpretation is not the one used by the compiler. The point was to catch trivial bugs in code. In this case, the interview didn't.