试图理解字符数组。 [英] Trying to understand character arrays.
问题描述
有没有人可以提供一些建议:
尝试在C中使用字符数组,下面的代码生成
此错误。
有人看错了吗?
提前谢谢。
Could anyone please offer some advice:
Trying to play with character arrays in C, the code below generates
this error.
Anyone see something wrong?
thank you in advance.
>>>>
#include< stdio.h>
void test(char s []);
char helloworld [11];
int main(){
printf("%s",test(helloworld)); //< ----无效使用void
表达式
返回0;
}
无效test(char t [])
{
t [0] =''H'';
t [1] = ''e'';
t [2] =''l'';
t [3] =''l'';
t [4] =''o'';
t [5] ='''';
t [6] =''W'';
t [7] =''o'';
t [8] =''r'';
t [9] =''l'';
t [10] =''d'';
t [11] =''\ 0'';
}
>>>
>>>> #include <stdio.h>
void test(char s[]);
char helloworld[11];
int main () {
printf("%s", test(helloworld)); // <----Invalid use of void
expression
return 0;
}
void test (char t[])
{
t[0]=''H'';
t[1]=''e'';
t[2]=''l'';
t[3]=''l'';
t[4]=''o'';
t[5]='' '';
t[6]=''W'';
t[7]=''o'';
t[8]=''r'';
t[9]=''l'';
t[10]=''d'';
t[11]=''\0'';
}
>>>
推荐答案
drM schrieb:
drM schrieb:
任何人都可以提供一些建议:
尝试在C中使用字符数组,下面的代码生成
这个错误。
任何人都看错了什么?
先谢谢你。
#include< stdio.h>
void test(char s []);
char helloworld [11]; >
int main(){
printf("%s",test(helloworld)); //< ----无效使用void
表达式
1)这很好地说明了为什么你不应该在
$ b $中使用//注释b usenet消息
2)test()的返回类型为void - 没有。
没有任何东西不能分配给任何东西。 (任何无效的东西)。
您可以将test()改为
char * test(char * s);
或者你必须分两步完成:
test(helloworld);
printf("%s",helloworld);
3)请注意,您应该输出''\ n''作为最后一个字符,以便确保您看到任何输出。返回0;
}
无效测试(char t [])
{0> =''H'';
t [1] =''e'';
t [2] =''l'';
t [3] =''l'';
t [4 ] =''o'';
t [5] ='''';
t [6] =''W'';
t [7] =''o' ';
t [8] =''r'';
t [9] =''l'';
t [10] =''d'';
t [11] =''\ 0'';
1)如果你想将test()更改为char * test(char *),那么
insert
return& t [0];
here
2)还有strcpy()和strncpy() ...
}
Could anyone please offer some advice:
Trying to play with character arrays in C, the code below generates
this error.
Anyone see something wrong?
thank you in advance.
#include <stdio.h>
void test(char s[]);
char helloworld[11];
int main () {
printf("%s", test(helloworld)); // <----Invalid use of void
expression
1) This nicely illustrates why you should not use // comments in
usenet messages
2) test()''s return type is void -- "nothing".
Nothing cannot be assigned to "anything" (anything non-void).
You can either change test() to
char *test (char *s);
or you have to do it in two steps:
test(helloworld);
printf("%s", helloworld);
3) Note that you should output ''\n'' as last character in order
to make sure that you see any output at all. return 0;
}
void test (char t[])
{
t[0]=''H'';
t[1]=''e'';
t[2]=''l'';
t[3]=''l'';
t[4]=''o'';
t[5]='' '';
t[6]=''W'';
t[7]=''o'';
t[8]=''r'';
t[9]=''l'';
t[10]=''d'';
t[11]=''\0'';
1) If you want to change test() to char *test(char*), then
insert
return &t[0];
here
2) There are also strcpy() and strncpy()...
}
干杯
Michael
-
电子邮件:我的是/ at / gmx / dot / de地址。
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
drM写道:
<可以请任何人提供一些建议:
尝试在C中使用字符数组,下面的代码生成
这个错误。
任何人都看错了?
[...] void test(char s []);
char helloworld [11];
[...] printf("%s",test(helloworld)); //< ----无效使用void表达式
Could anyone please offer some advice:
Trying to play with character arrays in C, the code below generates
this error.
Anyone see something wrong? [...] void test(char s[]);
char helloworld[11]; [...] printf("%s", test(helloworld)); // <----Invalid use of void expression
[...]
提示1:错误说什么?
提示2:test()返回什么类型,以及它与(1)的关系如何?
提示3:(2)中的类型可以回答传递给函数?
提示4:printf的'%s'是什么?期待?
-
+ ------------------------- + -------------------- + ---------------------------- - +
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody / at\spamcop.net | www.fptech.com | #include< std_disclaimer.h> |
+ ------------------------- + -------------- ------ + ----------------------------- +
不要给我发电子邮件:< mailto:Th ************* @ gmail.com>
[...]
Hint 1: What does the error say?
Hint 2: What type does test() return, and how does that relate to (1)?
Hint 3: Can the type answered in (2) be passed to a function?
Hint 4: What does printf''s "%s" expect?
--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don''t e-mail me at: <mailto:Th*************@gmail.com>
刚开始在C中,所以非常欢迎你的回复。
" 1)这很好地说明了为什么你不应该在
usenet messages中使用//注释....采取的点。
Have just started in C, so your reply is most welcome.
"1) This nicely illustrates why you should not use // comments in
usenet messages "....Point taken.
这篇关于试图理解字符数组。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!