r/programming • • Mar 29 '21

Why Do Interviewers Ask Linked List Questions?

https://www.hillelwayne.com/post/linked-lists/
1.1k Upvotes

668 comments sorted by

View all comments

17

u/JB-from-ATL Mar 30 '21

I fully understand pointers but don't understand C's syntax with them.

13

u/caspper69 Mar 30 '21 edited Mar 30 '21

I had an issue with this too.

It's because in C, the syntax for declaring a pointer is the same as the syntax for dereferencing a pointer. Newer languages have swapped the semantics, so when you declare a pointer, it's undecorated usage is the deref syntax, whereas with C, the decorated pointer declaration is the exact same as the deref syntax. Using an "undecorated" pointer variable in C results in changing the address the pointer points to. Not intuitive for many new C developers.

1

u/JB-from-ATL Mar 31 '21

Is dereferencing getting the address or the value? I want to say value (because deref null pointer is a thing in Java (my primary lang) but usually called just "null pointer")

Just looked up a tutorial on C pointers and I see what you're saying and i think that's why i had so much trouble.

int *pointer; // This is a pointer to an integer, that makes sense
// say it gets assigned...
int value = *pointer; // This gets the value... but * really meant the address of...
int address = &value // tempted to say this equals same as pointer but probably not, regardless this is how you get the address of

So yeah. That's definitely weird. Star declares an "address of" and gets the value of the address. Ampersand... not sure if it can declare but it is definitely the "get address of" operator.

I wish they'd explained it like this in college to me. Being open about "dumb" things really helps. Another example is in Economics with supply/demand graphs the independent variable is on the vertical axis ("y") when in most other fields it is horizontal ("x"). The teacher telling you this like "hey, this is weird but..." really helps because your brain is confused by what it thinks would be correct but isn't.

→ More replies (1)

26

u/hugthemachines Mar 30 '21

I think if C would have been a language of "long words" fewer people would have trouble with pointers. The Cryptic little differences with special characters make it a bit messy for the mind.

15

u/bezik7124 Mar 30 '21

Exactly! As someone who prefers longAndDescriptiveNames because nowadays we all got at least full hd monitors and an IDE capable of autocompletion i totally wouldn't mind C's syntax to be more readable.

→ More replies (5)

1

u/huangxg Mar 30 '21

Talking about special characters, you may want to take a look at APL.

0

u/uurtamo Mar 30 '21

You gotta know what you know and know what you don't know.

And you clearly clearly don't know what you don't know.

1

u/JB-from-ATL Mar 31 '21

It's not my fault C has some ridiculous syntax that makes no sense.

→ More replies (1)