typedef结构说明

时间:2016-12-05 13:23:59

标签: c struct typedef

任何人都可以解释一下这个有什么区别:

private void ContextMenu_Click(object sender, RoutedEventArgs e)
    {
        ContextMenu contextMenu = sender as ContextMenu;
        GridViewColumnHeader header = contextMenu.PlacementTarget as GridViewColumnHeader;

        if (e.Source == btnAsc)
        {
            Sort(header, true);
        }
        else
        {
            Sort(header, false);
        }
    }

和此:

typedef struct{
 char a[10];
 int b;
 char c[8];
 ...
}test;

由于

4 个答案:

答案 0 :(得分:5)

typedef struct{
 char a[10];
 int b;
 char c[8];
 ...
}test;

上面定义了一个匿名结构,并立即typedef为类型别名test

typedef struct test{
 char a[10];
 int b;
 char c[8];
 ...
}test;

然而,这会创建一个名为struct test的结构,并为其添加typedef

在第一种情况下,如果需要,您将无法转发声明struct 还有一个philosophy(我碰巧赞同这一点),默认情况下typedef所有结构都会降低代码的可读性,应该避免使用。

答案 1 :(得分:3)

在两个不同的地方进行“测试”有点令人困惑。我经常写这样的代码:

typedef struct test_s {
    ...
} test;

现在,我可以使用struct test_s类型,也可以只使用test。虽然单独test通常就足够了(在这种情况下你不需要test_s),但是你不能向前指定它:

// test *pointer; // this won't work
struct test_s *pointer; // works fine

typedef struct test_s {
    ...
} test;

答案 2 :(得分:0)

使用第一个版本,您只能声明:

test t;

使用第二个versijon,你可以选择:

struct test t;
test t;

答案 3 :(得分:-3)

简答:他们是相同的(在你的代码中)

答案很长:为什么要将test放在typedef struct{之间?那没有意义吗?

这个(结构名称test)在您的代码中毫无意义

但是,在此代码中,它不是:

struct Node {
  int data;
  struct Node * next;
} head;
相关问题