结构中的奇数字符串字符串初始化 [英] Odd char string initialization in structs

查看:72
本文介绍了结构中的奇数字符串字符串初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看Sendmail的源代码,我对这种初始化感到困惑




- ----------------------

struct prival PrivacyValues [] =

{

{"公开],PRIV_PUBLIC},

{" needmailhelo" ;, PRIV_NEEDMAILHELO},

{" needexpnhelo" ;, PRIV_NEEDEXPNHELO},

{" needvrfyhelo",PRIV_NEEDVRFYHELO},

....

};

------- -----------------


我熟悉char string [] =" foo",但我不知道代码剪辑上面的内容是什么?任何人都可以请解释

解决方案


古斯塔沃写道:我正在看Sendmail的源代码,我对这种初始化感到困惑:

-------------- ----------
struct prival PrivacyValues [] =
{
{" public",PRIV_PUBLIC},
{" needmailhelo",PRIV_NEEDMAILHELO} ,
{" needexpnhelo" ;, PRIV_NEEDEXPNHELO},
{" needvrfyhelo" ;, PRIV_NEEDVRFYHELO},
...
};
------ ------------------

我熟悉char string [] =" foo",但我不知道<上面的代码片段正在做。有人可以解释一下吗?




它只是结构数组的初始化。

它可以在你的代码中以另一种方式完成。


例如:

PrivayValues [0] = .field1"公共英寸;

PrivayValues [0] = .field2 PRIV_PUBLIC;


PrivayValues [1] .field1 =" needmailhelo";

PrivayValues [1] .field2 = PRIV_NEEDMAILHELO;


-----

PrivayValues [n] .field1 =" - " ;;

PrivayValues [n] .field2 = - ;


你能提供什么是struct prival ..


我想它一定是


struct prival

{

char * x;

int y;

在上面的例子中,

结构的一个元素可以使用
创建
struct prival OneElement = {" ; anystring",ANY_INTEGER};


第一个成员可能会感到困惑。它的作用是

1.商店anystring进入字符串表。

2.将指针放在OneElement.x中

在C程序中,如果出现字符串常量,则将它们替换为

指针和字符串常量被移动到字符串表。


但是,数组初始化是一个例外。


当我们写string [] =" foo",

时,编译器实际上将其视为


string [4] = {''f'' ,''o',''o'',''\ 0''};


因此foo根本没有去字符串表。

问候,

Yada Kishore


gustavo写道:

我正在查看Sendmail的源代码,我对这种初始化感到困惑:

--- ---------------------
struct prival PrivacyValues [] =
{
{" public",PRIV_PUBLIC},
{" needmailhelo",PRIV_NEEDMAILHELO},
{" needexpnhelo",PRIV_NEEDEXPNHELO},
{" needvrfyhelo",PRIV_NEEDVRFYHELO},
...
} ;
------------------------

我熟悉char string [] =" foo,但我不知道上面的代码片段是做什么的。有人可以解释一下吗?




char string [] =" foo" ;;


实际上只是一个方便的快捷方式更一般的形式:


char string [] = {

''f'',

''o'' ,

''o'',

''\ 0''

};


一般形式适用于C中的所有类型,例如:


int示例[] = {

100,

200,

$

};


这包括结构,例如:


struct thing {

char * name;

int size;

};


struct thing stuff [] = {

{" Me",100},

{" Myself",200},

{" I",300}

};


I was looking at the Sendmail''s source code, and i''ve got confused
about this kind of initialization:

------------------------
struct prival PrivacyValues[] =
{
{ "public", PRIV_PUBLIC },
{ "needmailhelo", PRIV_NEEDMAILHELO },
{ "needexpnhelo", PRIV_NEEDEXPNHELO },
{ "needvrfyhelo", PRIV_NEEDVRFYHELO },
....
};
------------------------

I''m familiar with char string[] = "foo", but i have no idea what the
above snip of code is doing. Anyone could please explain?

解决方案


gustavo wrote:

I was looking at the Sendmail''s source code, and i''ve got confused
about this kind of initialization:

------------------------
struct prival PrivacyValues[] =
{
{ "public", PRIV_PUBLIC },
{ "needmailhelo", PRIV_NEEDMAILHELO },
{ "needexpnhelo", PRIV_NEEDEXPNHELO },
{ "needvrfyhelo", PRIV_NEEDVRFYHELO },
...
};
------------------------

I''m familiar with char string[] = "foo", but i have no idea what the
above snip of code is doing. Anyone could please explain?



It is just the initialization of an array of structure.
It can be done in another way in your code.

Eg:
PrivayValues[0].field1 = "public";
PrivayValues[0].field2 = PRIV_PUBLIC;

PrivayValues[1].field1 = "needmailhelo";
PrivayValues[1].field2 = PRIV_NEEDMAILHELO;

-----
PrivayValues[n].field1 = "--";
PrivayValues[n].field2 = --;


can you also provide what is struct prival..

I suppose it must be

struct prival
{
char *x;
int y;
};
In the above case,
one element of the struct can be created using

struct prival OneElement = { "anystring", ANY_INTEGER };

The first member might be confusing. What it does is
1. Stores "anystring" into the string table.
2. Puts a pointer in OneElement.x

In a C program whereever string constants occur, they are replaced by
pointers and the string constants are moved to the string table.

But, array initialization is an exception to that.

when we write string[] = "foo",
the compiler actually takes it as

string[4] = { ''f'', ''o'', ''o'' , ''\0'' };

and therefore "foo" does not go to the string table at all.
Regards,
Yada Kishore


gustavo wrote:

I was looking at the Sendmail''s source code, and i''ve got confused
about this kind of initialization:

------------------------
struct prival PrivacyValues[] =
{
{ "public", PRIV_PUBLIC },
{ "needmailhelo", PRIV_NEEDMAILHELO },
{ "needexpnhelo", PRIV_NEEDEXPNHELO },
{ "needvrfyhelo", PRIV_NEEDVRFYHELO },
...
};
------------------------

I''m familiar with char string[] = "foo", but i have no idea what the
above snip of code is doing. Anyone could please explain?



char string[] = "foo";

is actually just a convenient shortcut of the more general form:

char string[] = {
''f'' ,
''o'' ,
''o'' ,
''\0''
};

The general form applies to all types in C, for example:

int example[] = {
100,
200,
300
};

this includes structs, for example:

struct thing {
char *name;
int size;
};

struct thing stuff[] = {
{"Me", 100},
{"Myself", 200},
{"I", 300}
};


这篇关于结构中的奇数字符串字符串初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆