WOndering why this would work

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is a version of some arduino code from a book I have. It compiles in the IDE, but fails spectacularly in Pelles C. I'm just wondering how it can ever work.

 




void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
int a =12;
  int b =21;
  printf("%d,%d,\n",a,b);
  swp(a,b);
  printf("%d,%d,\n",a,b);

}

void swp(int &x1, int &x2)
{
    int temp =0;
    temp =x1;
    x1 = x2;
    x2 = temp;

}

Pelles has multiple complaints about using &x1 and & x2( as wll it should).  Purpose was to get multiple results from a function.

 

Just curious,

 

hj

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

References (int&) are a C++ feature, so a "plain" C compiler will report an error.

 

--Mike

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Probably time to switch from Pelles to Microsoft if you want to compile C++ for "free".

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In C you would need to use pointers instead of references, which are a C++ feature, as already mentioned in #2.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

References are just one of the many things that makes a switch to C++ worthwhile so I'd follow the C++ route if I were you. Arduino is just C++ with a bit of icing on top after all!