0

Possible Duplicate:
Arrow operator (->) usage in C

As far as i know only C++ can use classes(obj->something) however i have seen this operator in numerous C applications.

And a small side-question. Usually one uses structures in C like this:

structname.somevariable

However i have seen them used like:

structname.something1.something2

Does it have something to do with the keyword union?

Community
  • 1
  • 1
dikidera
  • 2,004
  • 6
  • 29
  • 36

7 Answers7

7
struct A
{
    int b;
};

struct A *a;

a->b  ==  (*a).b;

And no, that has nothing to do with unions. It's just getting a member of a member.

user541686
  • 205,094
  • 128
  • 528
  • 886
5

if you have a pointer to a struct object, like

 struct P * p;

you access members with ->

 p->member

if p is not a pointer, you access members with .

struct P p;
p.member

Any C book covers this :P

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
2

Operator -> is used to access a member of a struct through a pointer to that structure. The second part of your question is used when accessing nested structures, it is not restricted to the use of unions. For instance:

struct A {
  int a;
};

struct B {
  struct A baz;
};

int main() 
{
  struct A foo;
  struct B bar;

  (&foo)->a = 10;

  bar.baz.a = 20;

  return 0;
}
Praetorian
  • 106,671
  • 19
  • 240
  • 328
0

C++ classes are an extension of C structs, and an object is just a pointer to a struct. C++ didn't actually invent a whole lot of new stuff; it's called C ++ for a reason.

structs can be nested. Given

struct a {
    int a_a;
    int a_b;
}

struct b {
    int b_a;
    struct a b_b;
} a;

you can refer to a.b_b.a_b.

A union uses similar syntax to a struct, but all the members overlap each other. This is useful when you need to interpret a chunk of memory in multiple ways.

I strongly suggest picking up a C book.

geekosaur
  • 59,309
  • 11
  • 123
  • 114
0

x.y.z is used when you want to access a member of a struct that is itself a member of another struct.

typedef struct _POINT
{
    int x;
    int y;
} POINT;

typedef struct _RECT
{
    POINT topLeft;
    POINT bottomRight;
} RECT;

int main()
{
    RECT r;
    r.topLeft.x = 0;
    t.topLeft.y = 0;

    int width = r.bottomRight.x - r.topLeft.x;
}
James
  • 9,064
  • 3
  • 31
  • 49
0

I've written a small example in C.

#include <stdio.h>
#include <stdlib.h>

struct a_t {
    int a;
    /* ... */
};

struct b_t {
    struct a_t a;
    /* ... */
};

int main()
{
    struct a_t a;
    struct a_t *b;
    b = malloc(sizeof(struct a_t));
    a.a = 1;
    b->a = 2;
    printf("%d; %d;\n", a.a, b->a); 
    (&a)->a = 3;
    (*b).a = 4;
    printf("%d; %d;\n", a.a, b->a);
    free(b);

    struct b_t c;
    c.a.a = 5;
    printf("%d;\n", c.a.a);
    return 0;
}
Michas
  • 8,534
  • 6
  • 38
  • 62
0

If I remember correctly, in C++

class IDENTIFIER {
    // stuff, possibly including functions
};

is exactly the same as

struct IDENTIFIER {
    private:
    // stuff, possibly including functions
};

and

struct IDENTIFIER {
    // stuff, possibly including functions
};

is exactly the same as

class IDENTIFIER {
    public:
    // stuff, possibly including functions
};
pmg
  • 106,608
  • 13
  • 126
  • 198