本文介绍了字符串数组。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在程序中,我可以这样声明字符串: 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! Inother words, the result of printf ("%s", str_table[0]) is:some stringsome other stringCan anyone explain why both aren''t equivalent?Thanks!推荐答案 他们 - 终止。试试这个程序: int main(void){ char * str_table [] = { " some string", 其他一些字符串 }; printf("%s \ n",str_table [0]); 返回0; } - 好的,只有流行语。两个音节,上衣。 - Laurie AndersonThey -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 您的程序中可能出现了打印 错误吗?例如, 如果你在第一个字符串令牌之后留下逗号 那么结果将是你所描述的,因为 字符串文字那个邻近的令牌是 连接。否则你的 代码片段看起来很好。 看到之间的区别: char * str_table [] = { " some string", " some other string" }; 和: char * str_table [] = { " some string" " some other string" }; ? - 希望这有帮助, 史蒂文Might you have made a typographicalerror in your program? For example,if you left off the comma after the first string tokenthen the results would be what you described becausestring literals that are adjacent tokens areconcatenated. Otherwise yourcode 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 他们是。你必须在你的程序中犯了一些其他错误。请 发布展示此行为的完整程序,我们将帮助您解决错误。 - John Gordon它肯定没有被奶酪污染。 去**** @ panix.comThey are. You must have made some other error in your program. Pleasepost the complete program that exhibits this behavior, and we will helpyou solve the error.--John Gordon "It''s certainly uncontaminated by cheese." go****@panix.com 这篇关于字符串数组。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 23:16