字符串数组。 [英] Array of strings.

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

问题描述




在程序中,我可以这样声明字符串:


char * some_str =" some string" ;;

char * some_str2 =" some other string" ;;


我可以用strlen等自然地处理它们。


我以为我也可以做以下事情:


char * str_table [] = {

" some string",

" some other string"

};


但是,我意识到每个声明的字符串都没有终止!在

中,printf("%s",str_table [0])的结果是:


一些其他字符串


谁能解释为什么两者都不等同?


谢谢!

Hi,

In a program, I can declare strings that way:

char *some_str = "some string";
char *some_str2 = "some other string";

I will be able to work on them very naturally with strlen and such.

I thought I could also do the following:

char *str_table[] = {
"some string",
"some other string"
};

But then, I realized that each declared string is not terminated! In
other words, the result of printf ("%s", str_table[0]) is:

some stringsome other string

Can anyone explain why both aren''t equivalent?

Thanks!

推荐答案

在文章< 11 ********************* @ k70g2000cwa.googlegroups中。 com>,
fu*******@gmail.com < fu *** ****@gmail.com写道:
In article <11*********************@k70g2000cwa.googlegroups. com>,
fu*******@gmail.com <fu*******@gmail.comwrote:

>在一个程序中,我可以用这样的方式声明字符串:
char * some_str =" some string" ;;
char * some_str2 =" some other string" ;;
>In a program, I can declare strings that way:
char *some_str = "some string";
char *some_str2 = "some other string";


>我将能够非常自然地使用strlen等工作。
>I will be able to work on them very naturally with strlen and such.


>我想我也可以做以下事情:
>I thought I could also do the following:


> char * str_table [] = {

" some string",

" some other string"
};
>char *str_table[] = {
"some string",
"some other string"
};


>但是,我意识到每个声明的字符串都没有终止!换句话说,printf("%s",str_table [0])的结果是:
>But then, I realized that each declared string is not terminated! In
other words, the result of printf ("%s", str_table[0]) is:


> some串其他字符串
>some stringsome other string


>任何人都可以解释为什么两者都不等同?
>Can anyone explain why both aren''t equivalent?



他们 - 终止。试试这个程序:

int main(void){

char * str_table [] = {

" some string",

其他一些字符串

};

printf("%s \ n",str_table [0]);

返回0;

}

-

好​​的,只有流行语。两个音节,上衣。 - Laurie Anderson

They -are- terminated. Try this program:
int main(void) {
char *str_table[] = {
"some string",
"some other string"
};
printf ("%s\n", str_table[0]);
return 0;
}
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson


fu ******* @ gmail .com 写道:


(剪断)
fu*******@gmail.com wrote:

(snipped)

我以为我也可以做以下事情:


char * str_table [] = {

" some string",

" some other string"

};


然后,我意识到每个声明的字符串都没有终止!在

中,printf("%s",str_table [0])的结果是:


一些其他字符串


谁能解释为什么两者都不等同?
I thought I could also do the following:

char *str_table[] = {
"some string",
"some other string"
};

But then, I realized that each declared string is not terminated! In
other words, the result of printf ("%s", str_table[0]) is:

some stringsome other string

Can anyone explain why both aren''t equivalent?



您的程序中可能出现了打印

错误吗?例如,

如果你在第一个字符串令牌之后留下逗号

那么结果将是你所描述的,因为

字符串文字那个邻近的令牌是

连接。否则你的

代码片段看起来很好。


看到之间的区别:


char * str_table [] = {

" some string",

" some other string"

};


和:


char * str_table [] = {

" some string"

" some other string" < br $>
};





-

希望这有帮助,

史蒂文


Might you have made a typographical
error in your program? For example,
if you left off the comma after the first string token
then the results would be what you described because
string literals that are adjacent tokens are
concatenated. Otherwise your
code snippet looks fine.

See the difference between:

char *str_table[] = {
"some string",
"some other string"
};

and:

char *str_table[] = {
"some string"
"some other string"
};

?

--
Hope this helps,
Steven


In< 11 ******************** *@k70g2000cwa.googlegroups。 COM"福******* @ gmail.com" < fu ******* @ gmail.comwrites:
In <11*********************@k70g2000cwa.googlegroups. com"fu*******@gmail.com" <fu*******@gmail.comwrites:

谁能解释为什么两者都不等同?
Can anyone explain why both aren''t equivalent?



他们是。你必须在你的程序中犯了一些其他错误。请

发布展示此行为的完整程序,我们将帮助您解决错误



-

John Gordon它肯定没有被奶酪污染。
去**** @ panix.com

They are. You must have made some other error in your program. Please
post the complete program that exhibits this behavior, and we will help
you solve the error.

--
John Gordon "It''s certainly uncontaminated by cheese."
go****@panix.com


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

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