Pages: [1]   Go Down
Author Topic: Proper language documentation available?  (Read 1295 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 111
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://arduino.cc/en/Reference/Pointer is rather scant on information. Can anyone point me to a resource which explains this?

I fould an example somewhere where & (reference) and * (dereference) is used to pase a parameter by reference, but I have run into problems trying to pas a parameter by reference when the parameter was received by reference....

help

When I enter

Code:
void processMotor(String* inString) {
String myWord;

//read word and process
while (&inString != '') {
myWord = getNextWord(inString);

I get the following trying to compile.

Code:
ServoMotorTest.cpp:117:22: error: empty character constant

Which I have narrowed down to the where line.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50300
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
   while (&inString != '') {
Values are passed to functions by value or passed by reference. In the absence of the &, values are passed by value.

Inside the function, how the data was passed is completely irrelevant.

So, why do you have an & in front of inString? In this context, & means address of. So, you are (trying to) compare the address of inString to ''. Does that really make sense?

Quote
I fould an example somewhere where & (reference) and * (dereference)
The * is not dereference. It indicates a pointer. The variable is a pointer to data of the specified type.

Quote
but I have run into problems trying to pas a parameter by reference when the parameter was received by reference.
Once more, please, in English.

Parameters are passed by reference, not received be reference.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 111
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

nevermind...

I have done the following.

Code:
void processMotor(String* inString) {
String myWord;
String myInString = *inString;

//read word and process
while (myInString != "") {
    myWord = getNextWord(&myInString);

lol - Thanks for the response Paul. It's late and I have been at it since 8 yesterday morning. I should go to bed now...
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50300
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you pass the string by reference, what is passed is the location of the String object. There is, then, no reason to copy the string.

In case you aren't aware:
Code:
String myInString = *inString;
invokes the copy operator, defeating the purpose of passing be reference, rather than by value.

Of course, code snippets never tell the whole story.
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This tutorial helped refresh my member on pointers:

http://pweb.netcom.com/~tjensen/ptr/cpoint.htm

I keep the PDF on my tablet for quick reference.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

0
Offline Offline
Shannon Member
****
Karma: 215
Posts: 12467
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The * is not dereference. It indicates a pointer.

* means both - in variable declarations it indicates a pointer type, in expressions it means dereference (a pointer).

Quote
Parameters are passed by reference, not received be reference.

That doesn't make sense - the receiver (the 'callee') has to know how the caller passed the variable, so if it is passed by reference then it must be received as a reference.  If caller and callee disagree on the calling protocol then the result could be nonsense!

The '&' operator means 'address of', the & type operator means 'pass by reference'.  The '*' type operator means 'pass by value', but that the value will be a reference (a pointer).  The distinction can be confusing to newcomers of course - but the rule is if you use '&' (pass by reference) then the compiler knows to automatically dereference appropriately.  If you pass a pointer then the compiler leaves that task to you to get right.  The type system means that you will get warnings or errors if you get confused.

Code:
void foo (int & x)
{
  x++ ;  // increment the variable that is passed by reference - x is an alias for the actual variable in the call.
}

void bar (int y)
{
  y++ ;  // increment the local variable y - this has no effect on any other variable. (this example has no useful effect at all!)
}

void baz (int * z)
{
  (*z)++ ;  // increment the value in the address held in the (pass-by-value) variable z
}

void main ()
{
  int a = 0 ;
  bar (a) ; // pass a's value, no change to a
  foo (a) ; // pass a by reference, a is incremented
  baz (&a) ; // pass a's address by value, a is incremented.

  bar (7) ; // legal, nothing useful results(!)
  foo (7) ; // not legal - the parameter must be a variable
  baz (7) ; // not legal, a type mismatch
  baz ((int *) 7) ; // typecast makes this legal, but unless you know what is at address 7, this is nonsense code.
}

Note that foo and
Code:
baz
will typically be compiled to exactly the same object code...
Logged

[ I won't respond to messages, use the forum please ]

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26495
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
while (&inString != '') {
Quote
error: empty character constant

I don't find that surprising.
An empty character is not the same as an empty string.
« Last Edit: August 07, 2011, 03:28:48 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: