What does "->" mean/do in code?

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

I am interested in writing a PID loop for a project I am working on and was looking at the AVR221 application note discrete PId controller. I was having a hard time finding the "included" code but found it here:

https://code.oregonstate.edu/svn/appliedrobotics/ExampleCode/pid_demo/pid.c

It often uses this "->" operation. What exactly does this do?

pid->sumError = 0;




else if (error < -pid_st->maxError)

I also see it used here on page 8
http://www.eetimes.com/ContentEETimes/Documents/Embedded.com/2000/f-wescot.pdf

Never seen this before and am having a hard time finding any discussions about this anywhere.

Last Edited: Mon. Nov 7, 2011 - 03:14 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

What exactly does this do?

Struct pointer dereference.

Consider:

typedef struct {
  int n;
  char c;
} st_type;

st_type astruct;
st_type * pointer_to_astruct;

int main(void) {
  astruct.n = 12345;
  pointer_to_astruct = &astruct; //pointer gets address of struct
  pointer_to_astruct->c = 'A';
  if (pointer_to_struct->n = 12345) {
     PORTB = astruct.c;
  }
}

That alternates use of a dereferenced struct pointer and direct struct element access to show that the two can be used almost interchangeably.

The reason struct pointers and the derefernece operator are most often used is when the address of a struct is passed in to a function:

void process_data(st_type * p) {
  if (p->n == 12345) {
    PORTB = p->c;
  }
}

st_type mystruct;

int main(void) {
   mystruct.c = 'A';
   mystruct.n = 12345;
   process_data(&mystruct);
}

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

Quote:

Never seen this before and am having a hard time finding any discussions about this anywhere.

You really have no reference material at all on the C programming language?

You need to hone your searching skills:
How about "C operators"? E.g. Goole quickly will lead you to stuff like this.

For a text book on C that you can read on the web, or even download as a PDF (legally!), go here.

Quote:
I was having a hard time finding the "included" code

It is right next to the app note as such on Atmels web page. The disk icon leads to a ZIP file with the code. (You are not the first to be confused by this - IMO a document icon with ".c" or ".asm", or perhaps even ".zip", would heve worked better.)

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

It's true that you cannot Google "->" and you kind of need to do that to know that your real search term is "structure pointer dereference" but I agree that anyone trying to use C should have at least one book on the subject (preferably K&R).

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

There is a fantastic little "cheat sheet" :

https://www.avrfreaks.net/index.p...

This will, at least, tell you what something is (in c language) so you can look up the details in the excellent book Johan referenced, or other sources.

To be fair, I took a "c programming" class (one academic quarter) and the instructor did not get to "advanced" things like structures until the very end. These things were not really used in any practice coding, and I still have to scratch my head a bit, whenever this construction appears.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

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

Cliff wrote:
K&R
Chapter 6.2 (at least in my "localized" copy).

It is stressed that it is a convenience replacement for (*p).q - apparently it was easier to type '-' and '>' on PDP11's keyboard, than '(' and ')' :-) .

There is quite a lot of arcane stuff in the preceding chapters and I gave up a lot earlier (at around 5.4). And I even had neutral emotional relationship towards C back then... ;-)

Jan

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

I didnt see anything about gcc in this question and answer session either. If the gcc forum is the new place to ask your General C questions and get them answered, lets change the forum description?

Imagecraft compiler user

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

Just ask a mod to move it then, Bob.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

gcc :D

Gary - W4GNS
Tel: BR549

In my many years I have come to a conclusion that one useless man is a shame, two is a law firm and three or more is a congress. -- John Adams

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

When you don't know what category a question even falls into, then picking a forum is a problem!

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net