4
C-sucks
3y

Hey developers, am I allowed to make use of the pass-by-reference feature of C/C++ during a coding interview( given I am using C/C++ as my main language )?

I basically used python in my interviews, but this time I decided to go with C/C++.

now,
for those who gonna say "WRONG CATEGORY": most of you check rant rather than questions.

for those who gonna say "BUT YOUR NAME SUGGEST THAT YOU HATE C": bloody educate yourself.

Comments
  • 9
    You anticipated all of my petty answers, well done
  • 11
    No, you cannot pass by reference during coding interview. ISO/IEC 14882:2020 explicitly forbids this.
  • 3
    Of course you can use pointers, there's a reason why they are there. Idiomatic C hands larger data in and out via pointers and uses the return value for error codes. Just have a look at the standard library.

    It's just that a function prototype like
    void func(int *result);
    is pretty silly if it points to a single int because you can as well return the value directly, and if points to an array, it's bad because there's no buffer length information handed over. Btw., buffer length is always of type size_t, not int.

    Extra points if you use the const qualifier on pointer parameters whenever possible, and for extra performance, the restrict qualifier (C99+ only) where applicable. More extra points if you can explain what const actually means (hint: it doesn't mean constant).
  • 5
    Then the other part: no, you don't use raw pointers, like, at all. Idiomatic C++ doesn't use them because there's no point in incurring the huge language complexity of C++ and then not even making use of its advantages. Don't write C code that pretends to be C++. References are a different story, of course.

    So, it really depends on whether you go for C or C++. They're not the same.
  • 2
    @Fast-Nop Oi thanks mate, for your answer.
    I reckon const stands for constant only. If not that, what does it actually mean, or better if you can provide me with some piece of code that defines const doesn't mean constant..
  • 1
    @devnulli now that's a quality piece of advice.
    thanks, mate.
  • 2
    @C-sucks "const" tells the compiler that this variable does not change programmatically, i.e. you don't write to it programmatically.

    It can however change non-programmatically, e.g. if your pointer points to some read-only I/O register that can change in hardware. In that case, you'd usually pair const with volatile.

    const on pointer function parameters doesn't mean they're constant - it just means that this function doesn't modify the data. That enables not only some optimisations, it also helps the next programmer to understand that part just from looking at the function prototype.
  • 2
    Wrong category
  • 0
    Sorry but going to give an answer to that just because you tagged it wrong on purpose.
    Same can be said for joke/meme
  • 2
    Why not? If anything, it's better to use references instead of pointers unless you really need a pointer.
  • 1
    Also. WRONG CATEGORY!
Add Comment