4 ms·
p contains whatever happens to be on the stack at the position assigned to it by the compiler. Basically that means that p could point anywhere, so by assigning
by amag 10y ago
p contains whatever happens to be on the stack at the position assigned to it by the compiler. Basically that means that p could point anywhere, so by assigning a char 'c' to a "random" address the best thing that could happen is a GPF/segfault. Most likely you'll spend weeks looking for a multitude of weird bugs that seemingly appear at different places out of nowhere, only happens sometimes and never in debug builds.
Yay, the joys of uninitialized variables!
- xorcist 10y agoIt could point anywhere, but the question here was where it does point to. And that's a remarkably unsuited question for a coding interview as it's going to depend on which compiler you use, which flags you have set, and whether the declaration is inside the same function as the reference. The C FAQ (when there was such a thing) used to contain a whole section on things like this. As a conversation starter it could perhaps be useful, if you can phrase it suitably. My own experience is that trick questions posed as if they had a singular answer will do strange things to nervous people. It's just doesn't tell you much. If I had this question dropped at me, my hunch would be that the interviewer had a slightly inflated idea of their own knowledge of the language and would likely not take kindly to being taken down. That might well colour my take on the answer. It's not always rewarding to argue minutiae.
- vostok 10y agoI'm not a developer and I think the question makes sense. I would have given a similar response to what amag wrote, although I would have skipped this bit: "Most likely you'll spend weeks looking for a multitude of weird bugs that seemingly appear at different places out of nowhere, only happens sometimes and never in debug builds." It's pretty clearly a conversation starter and there are lots of valid ways to answer the question. > If I had this question dropped at me, my hunch would be that the interviewer had a slightly inflated idea of their own knowledge of the language and would likely not take kindly to being taken down. That might well colour my take on the answer. If the interviewer responds poorly to this kind of answer then I don't want to work there anyway. Also, this kind of answer is not "taking down" anyone.
- GFK_of_xmaspast 10y agoI think it's a fine question. "Q: where does it point to? A: that depends," and move on.
- Klockan 10y ago> but the question here was where it does point to. No, the question was "what happens here" and the obvious answer is "you write 'a' to an undefined location, likely segfaulting". Since the candidate failed to spot this a hint was given hoping that the candidate would realize that the pointer didn't point at a valid location.
- Delmania 10y agoThis answer explains the real issue. The problem is the question is asking the person to find the bug in the code, but the way it's asked is in a way that would make someone feel like a freshman in college.
- eveningcoffee 10y agoI would say that an experienced C developer would blurt out that there is a bug in this code as an first reaction. Bugs like these are one reason why we can not have the nice things on Earth, and an experienced C developer should notice them even when not looking for a bug. Well, that is at least my impression, as I am not a one, it is just based on my experience with the language.