C 与结构有关的词汇
以我的例子为例:评论中概述了问题C 与结构有关的词汇,c,structure,C,Structure,以我的例子为例:评论中概述了问题 struct jeff { //jeff is the tag name correct? int age; int weight; }; typedef struct { // Is it correct to say that this structure has no "tag" name? int age; int weight; }jeff; // This is an alias f
struct jeff { //jeff is the tag name correct?
int age;
int weight;
};
typedef struct { // Is it correct to say that this structure has no "tag" name?
int age;
int weight;
}jeff; // This is an alias for an anonymous structure correct? Not its tag name.
typedef struct jeff { // Is it correct to say that this struct has a tag name jeff
int age; // and an alias Jeffery?
int weight;
} Jeffery;
这些问题实际上只是与C语言的正确语义有关
最后一个问题:
struct {
int age;
int weight;
}jeff; // Why would one want a struct with no name/alias. I don't see the benefit.
杰夫:标签名正确吗
对
说这个结构没有标记名是正确的吗
没错
这是匿名结构的别名,对吗?不是它的标签名
好吧,它叫做typedef,但是这里没有结构标签,只有一个typename
说这个结构有一个标记名jeff和一个别名Jeffery是否正确
如果你愿意的话
为什么要一个没有名称/别名的结构
也许只是为了好玩?也许它在某个地方是一个临时变量,在代码中的其他地方根本没有使用它,所以为它定义一个结构类型是多余的。不管怎样,我不喜欢这种风格
杰夫:标签名正确吗
对
说这个结构没有标记名是正确的吗
没错
这是匿名结构的别名,对吗?不是它的标签名
好吧,它叫做typedef,但是这里没有结构标签,只有一个typename
说这个结构有一个标记名jeff和一个别名Jeffery是否正确
如果你愿意的话
为什么要一个没有名称/别名的结构
也许只是为了好玩?也许它在某个地方是一个临时变量,在代码中的其他地方根本没有使用它,所以为它定义一个结构类型是多余的。无论如何,我不喜欢这种风格……用于匿名结构
匿名结构的数组可能有用的一个地方是在代码中,在一个函数之外不需要数据
const char *info_lookup(int value)
{
static const struct { int number; char *name; } list[] =
{
{ 1, "twenty-seven" },
{ 13, "unfortunately" },
{ 27, "won" },
...
};
enum { NUM_LIST_ITEMS = sizeof(list) / sizeof(list[0]) };
for (i = 0; i < NUM_LIST_ITEMS; i++)
{
if (value <= list[i].number)
return(list[i].name);
}
return(0);
}
我有时也使用它来运行测试,其中结构捕获测试信息:
static const struct
{
const char *ver1;
const char *ver2;
int result;
} test[] =
{
{ "7.0.4.27", "7.0.4.17", +1 },
{ "7.0.4.23", "7.0.4.17", +1 },
{ "7.0.4.23", "7.0.4.27", -1 },
{ "7.0.4.23", "7.0.5.07", -1 },
...20+ tests omitted...
};
enum { NUM_TESTS = DIM(test) };
static const char *result(int i)
{
if (i < 0)
return("<");
else if (i > 0)
return(">");
else
return("=");
}
int main(void)
{
size_t j;
int fail = 0;
for (j = 0; j < NUM_TESTS; j++)
{
int r1 = version_compare(test[j].ver1, test[j].ver2);
int r2 = version_compare(test[j].ver2, test[j].ver1);
const char *pass_fail = "PASS";
char extra[32] = "";
if (r1 != test[j].result)
{
pass_fail = "FAIL";
fail++;
snprintf(extra, sizeof(extra), " Expected %s", result(test[j].result));
}
assert(r1 == -r2);
printf("%s: %-10s %s %s%s\n",
pass_fail, test[j].ver1, result(r1), test[j].ver2, extra);
}
if (fail == 0)
{
printf("== PASS == %d tests passed\n", NUM_TESTS);
return(0);
}
else
{
printf("!! FAIL !! %d out of %d tests failed\n", fail, NUM_TESTS);
return(1);
}
}
它只适用于文件外部不需要了解结构的情况,以及只有一个类型的变量,或者可以在一个声明中声明该类型的所有变量,但我通常每个声明只有一个声明符,因此相当于该类型的一个变量
如果需要多次引用该类型,则它需要一个名称—结构标记或typedef名称或两者兼有。用于匿名结构
匿名结构的数组可能有用的一个地方是在代码中,在一个函数之外不需要数据
const char *info_lookup(int value)
{
static const struct { int number; char *name; } list[] =
{
{ 1, "twenty-seven" },
{ 13, "unfortunately" },
{ 27, "won" },
...
};
enum { NUM_LIST_ITEMS = sizeof(list) / sizeof(list[0]) };
for (i = 0; i < NUM_LIST_ITEMS; i++)
{
if (value <= list[i].number)
return(list[i].name);
}
return(0);
}
我有时也使用它来运行测试,其中结构捕获测试信息:
static const struct
{
const char *ver1;
const char *ver2;
int result;
} test[] =
{
{ "7.0.4.27", "7.0.4.17", +1 },
{ "7.0.4.23", "7.0.4.17", +1 },
{ "7.0.4.23", "7.0.4.27", -1 },
{ "7.0.4.23", "7.0.5.07", -1 },
...20+ tests omitted...
};
enum { NUM_TESTS = DIM(test) };
static const char *result(int i)
{
if (i < 0)
return("<");
else if (i > 0)
return(">");
else
return("=");
}
int main(void)
{
size_t j;
int fail = 0;
for (j = 0; j < NUM_TESTS; j++)
{
int r1 = version_compare(test[j].ver1, test[j].ver2);
int r2 = version_compare(test[j].ver2, test[j].ver1);
const char *pass_fail = "PASS";
char extra[32] = "";
if (r1 != test[j].result)
{
pass_fail = "FAIL";
fail++;
snprintf(extra, sizeof(extra), " Expected %s", result(test[j].result));
}
assert(r1 == -r2);
printf("%s: %-10s %s %s%s\n",
pass_fail, test[j].ver1, result(r1), test[j].ver2, extra);
}
if (fail == 0)
{
printf("== PASS == %d tests passed\n", NUM_TESTS);
return(0);
}
else
{
printf("!! FAIL !! %d out of %d tests failed\n", fail, NUM_TESTS);
return(1);
}
}
它只适用于文件外部不需要了解结构的情况,以及只有一个类型的变量,或者可以在一个声明中声明该类型的所有变量,但我通常每个声明只有一个声明符,因此相当于该类型的一个变量
如果您需要多次引用该类型,则它需要一个名称—结构标记或typedef名称,或两者兼而有之。可能会有所帮助……虽然帮助不大,但不够具体。可能会有帮助…它有一定的帮助,但不够具体。也许为了破解泛型参数,可以在宏中使用匿名结构:define generic\u foo。。。foo&struct{int x;char*y;double z;}{.x=0.y=NULL.z=0.0,{uuu VA_ARGS}后跟:generic_foo.x=10;无名结构对于避免数组的并行索引非常有用,例如结构{char*ptr;size_t len;}字符串[MAX_ARGS];也许为了破解泛型参数,可以在宏中使用匿名结构:define generic\u foo。。。foo&struct{int x;char*y;double z;}{.x=0.y=NULL.z=0.0,{uuu VA_ARGS}后跟:generic_foo.x=10;无名结构对于避免数组的并行索引非常有用,例如结构{char*ptr;size_t len;}字符串[MAX_ARGS];