C 中的这个运算符(->)是什么?

发布于 2024-11-18 16:59:28 字数 432 浏览 3 评论 0原文

可能的重复:
C 中箭头运算符 (->) 的用法

据我所知,只有 C++ 可以使用类(obj->something) 但是我在许多 C 应用程序中看到过这个运算符。

还有一个小问题。通常人们在 C 中使用这样的结构:

structname.somevariable

但是我见过它们这样使用:

structname.something1.something2

它与关键字 union 有关系吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

囍笑 2024-11-25 16:59:28
struct A
{
    int b;
};

struct A *a;

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

不,这与工会无关。这只是获得会员的会员。

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.

少年亿悲伤 2024-11-25 16:59:28

如果你有一个指向结构体对象的指针,就像

 struct P * p;

使用 -> 访问成员

 p->member

一样,如果 p 不是指针,则使用 访问成员。

struct P p;
p.member

任何 C 书籍都涵盖了这一点:P

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

少女情怀诗 2024-11-25 16:59:28

运算符 -> 用于通过指向该结构的指针来访问该结构的成员。您问题的第二部分在访问嵌套结构时使用,它不限于使用联合。例如:

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;
}

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;
}
染年凉城似染瑾 2024-11-25 16:59:28

C++ 类是 C 结构体的扩展,对象只是指向结构体的指针。 C++ 实际上并没有发明很多新东西;而是它被称为 C ++ 是有原因的。

结构体可以嵌套。鉴于

struct a {
    int a_a;
    int a_b;
}

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

您可以参考a.b_b.a_b

union 使用与 struct 类似的语法,但所有成员都相互重叠。当您需要以多种方式解释一块内存时,这非常有用。

我强烈建议拿起一本 C 书。

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.

梦年海沫深 2024-11-25 16:59:28

当您想要访问一个结构体的成员,而该结构体本身又是另一个结构体的成员时,可以使用 xyz。

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;
}

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;
}
痴者 2024-11-25 16:59:28

我用 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;
}

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;
}
空心↖ 2024-11-25 16:59:28

如果我没记错的话,在 C++ 中

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

完全 与 相同

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

,并且

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

完全

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

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
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文