Pages: 1 [2]   Go Down
Author Topic: Wacky unknown pointer issues.  (Read 1101 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

No, it isn't. Try this:

Code:
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:
1
1

Now change bar to be:

Code:
void bar (struct foo & x)

That prints:

Code:
1
42

So the default, as with all other arguments, is call by value.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: 1 [2]   Go Up
Jump to: