我有这个数组:
char alph[] ={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9','.',',',':','?','=','-','(',')','"','\'','/','%','@','!'};
可以使用'perc'这样的单词更改符号'%'吗?
感谢您的帮助!
答案 0 :(得分:6)
鉴于alph
的声明,每个元素都是单个char
,而字符串"perc"
由多个字符组成。但请注意,字符文字'perc'
(带单引号)是有效的C代码,但其值通常无用;如果你试图将它打印出来,你就会用垃圾而不是单词" perc&#34 ;.
如果您想在数组中使用"perc"
,那么您需要将数组的类型更改为const char *alph[]
,并将所有其他元素更改为使用双引号(即{ {1}}等。)
答案 1 :(得分:3)
您正在做的是将{em>单 char
替换为multicharacter literal
,这取决于实现。它可能有效,如何存储这些值取决于编译器,所以如果你这样做:
char[] a = {'a, 'b', 'c'};
a[0] = '123';
和
printf("%c", a[0]);
不打算像您一样打印123
。
答案 2 :(得分:2)
不,你可能没有,但你可以用字母'p'
...
你拥有的是一个char
大小的数组,包含50个元素,字符值为-128到127
你可以在char *
的数组中存储类似的数据,它们是指向具有空终止C字符串的内存位置的指针。
答案 3 :(得分:0)
正如其他人所指出的那样,你不能。
但是,如果您从alph
char
的定义
char alph[] ={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9','.',',',':','?','=','-','(',')','"','\'','/','%','@','!'};
指向char
char * alph[] ={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9",".",",",":","?","=","-","(",")","\"","'","/","%","@","!"};
这是可行的。在后一种情况下,只需:
alph[<index of %>] = "perc";