Go Down

Topic: Wacky unknown pointer issues. (Read 1 time) previous topic - next topic

BizarrePsycho

Will do. Was writing this as a library to maybe help with people making LED cubes since there are so many people making them.  Trying to add as much functionality as possible but I only have 1 cube to test it on so it's getting hard to keep of whether or not it will work on any size cube.

Nick Gammon


And I believe the default for passing a struct into a function is pass by reference. 


No, it isn't. Try this:

Code: [Select]
struct foo {
    int a;
    int b;
};

void bar (struct foo x)
  {
  x.a = 42;
  }
 
void setup ()
{
  struct foo y;
  y.a = 1;
  y.b = 2;

  Serial.begin (115200);
  Serial.println (y.a);
  bar (y);
  Serial.println (y.a);
  exit (0);  // little jest there
}
void loop () {}


That prints:

Code: [Select]
1
1


Now change bar to be:

Code: [Select]
void bar (struct foo & x)

That prints:

Code: [Select]
1
42


So the default, as with all other arguments, is call by value.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

BizarrePsycho

Alright. Changed it. No discernible difference, but can't hurt to be safe.  Thanks for the tip.  I'll work on it a bit more then maybe re-post in a different thread since it has nothing to do pointers anymore.

Nick Gammon

Depends what you mean by "safe". If you want the original to be changed, it is safe to pass by reference. If you want a copy, it isn't.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

BizarrePsycho

Safer in the fact that I know that the original will be changed if I change a value and not just hope it changes. There is a function I need to retool that I want to change all the values.

Go Up