Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
No, you cannot pass by reference during coding interview. ISO/IEC 14882:2020 explicitly forbids this.
-
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). -
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. -
C-sucks6943y@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.. -
@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. -
hjk10156963ySorry but going to give an answer to that just because you tagged it wrong on purpose.
Same can be said for joke/meme -
iiii90883yWhy not? If anything, it's better to use references instead of pointers unless you really need a pointer.
Related Rants
-
xjose97x19Just saw a variable in C named like this: long time_ago; //in a galaxy far away I laughed no stop.
-
elgringo41Student - Teacher renaming .c to .exe make the program executable ? Teacher - Yes A group of people stand up...
-
Unskipp24So this happened last night... Gf: my favorite bra is not fitting me anymore Me: get a new one ? Gf: but it ...
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.
rant
c++
pass by reference
coding interviews
c