Hacker News new | ask | show | jobs
by roscoebeezie 3522 days ago
As one who claims to know C on my resume, but I too am a < 3 month novice, lemme try to answer this one.

You declare some pointer and give it whatever value 'c' is?

I probably would have gotten the question wrong too and just saying "Get out" is kind of harsh.

3 comments

> I probably would have gotten the question wrong too and just saying "Get out" is kind of harsh.

If you can't do that, you don't understand pointers. If you don't understand pointers, you're going to write really buggy code.

I've never told someone to leave an interview early. That's a bit unprofessional in my view. But I certainly wouldn't hire a C programmer who doesn't understand pointers.

    char *p;
p doesn't point to anything[0]. You have declared a pointer to char but not given it any memory to point to. It is uninitialised.

    *p = 'c';
So what are you dereferencing here? You can't dereference something you haven't initialised.

[0] well it might point to something, it is up to the compiler as it is UB.

Edit: btw if you could not answer this very simple problem please do not advertise yourself as a C programmer as you are not yet. Learn C and then advertise yourself as a C programmer.

Yeah, one more thing. C and C++ type languages where you have to manage memory allocation are a different kind of intellectual challenge than languages like java or python. It takes a certain amount of try and failure and intellectual carefulness to get the ability to write trustworthy code. Just keep going at it. And be careful not to call yourself an expert c programmer, even after you get basic fluency. That's usually a sign that someone is not a great programmer in my experience :-)
Get out would be rude. If you interview a lot of people who say they are programmers, you get people who can't do anything sometimes. It is frustrating perhaps but that's no reasonable way to treat people.

Back to the question. In C or C++ the pointer is pointing to a memory location. If you don't set it to something (ie no malloc, or assignment, then it is uninitialized and you can't use what it points to. It is essentially a random number. Looking at a random memory location "(* p)" could likely crash your program, but it won't be anything useful that you should use. So when you say "* p" = 'c'; you say overwrite the memory location (which is randomly chosen) with the ascii code for the character 'c'.

Side note - the surprising markup language here doesn't let you use asterisk p directly, you have to put a space in I just discovered.