How do you pass an int by reference to a function?
-
int AnInt=6; void EditInt(int * IntPnt) { *IntPnt=7; } EditInt(&AnInt); Is this how you should proceed?
-
int AnInt=6; void EditInt(int * IntPnt) { *IntPnt=7; } EditInt(&AnInt); Is this how you should proceed?
That's passing as a pointer, not as a reference. As a reference (C++ only, though maybe C now has them too), it's
void EditInt(int& IntRef)
{
IntRef=7;
}int AnInt=6;
EditInt(AnInt);Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
That's passing as a pointer, not as a reference. As a reference (C++ only, though maybe C now has them too), it's
void EditInt(int& IntRef)
{
IntRef=7;
}int AnInt=6;
EditInt(AnInt);Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
That's passing as a pointer, not as a reference. As a reference (C++ only, though maybe C now has them too), it's
void EditInt(int& IntRef)
{
IntRef=7;
}int AnInt=6;
EditInt(AnInt);Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing.I got it, thank you.
-
Greg Utas wrote:
That's passing as a pointer, not as a reference.
Which is just the same, apart from semantics.
Very true. A reference can even be to
nullptr
, and you need to check for this if writing bulletproof code:void function(type& arg)
{
if(&arg == nullptr)
throw std::invalid_argument("function received null argument");
}type* nasty = nullptr;
function(*nasty);Without the check,
function
crashes when it gets around to usingarg
.Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
Very true. A reference can even be to
nullptr
, and you need to check for this if writing bulletproof code:void function(type& arg)
{
if(&arg == nullptr)
throw std::invalid_argument("function received null argument");
}type* nasty = nullptr;
function(*nasty);Without the check,
function
crashes when it gets around to usingarg
.Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
Very true. A reference can even be to
nullptr
, and you need to check for this if writing bulletproof code:void function(type& arg)
{
if(&arg == nullptr)
throw std::invalid_argument("function received null argument");
}type* nasty = nullptr;
function(*nasty);Without the check,
function
crashes when it gets around to usingarg
.Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing.I would
assert()
something like that rather thanthrow
. It strikes me as an error in the code itself, rather than something that should have filtered its way into runtime code, so I think assert is more appropriate. As I understand it, best practices indicate that a reference should never be deliberately null. Could be wrong here?Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix
-
I would
assert()
something like that rather thanthrow
. It strikes me as an error in the code itself, rather than something that should have filtered its way into runtime code, so I think assert is more appropriate. As I understand it, best practices indicate that a reference should never be deliberately null. Could be wrong here?Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix
What to do depends on the environment you're running in. I've never used
assert
, so I had to look up the documentation, which says that it callsabort
. Most application code won't catch aninvalid_argument
exception, so the effect will be similar. In my open-source software, I neitherthrow
norassert
in this situation. Instead, I generate a debug log with a traceback and return whatever signifies failure if the function has a result. That's appropriate if avoiding crashes is paramount, which is what my software aims for. You're right that a reference shouldn't be null. For many functions, a pointer argument shouldn't be null either. But applications misbehave, so you have to decide whether your function will check for bad arguments or just crash.Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing.