本文介绍了在函数之间传递字符串问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 尝试使用此代码来帮助我理解字符串和函数 但我不确定为什么会出现错误并希望有人可以 为我的学习提供了一些启示。 #include< stdio.h> #include< string.h> #include< ctype.h> #define STRINGSIZE 10 char isLowercase(char * string, char * string2); int main() { char string1 [STRINGSIZE]; char string2 [STRINGSIZE]; printf("请输入一个字符串进行检查:"); fgets(string1,STRINGSIZE,stdin ); isLowercase(string1,string2); printf(" Main String1 =%s",string1); printf(" Main String2 =%s",string2); 返回0; } char isLowercase(char * string1,char * string2) { / *复制来自的小写元素string1到string2。 * / int count1 = 0; int count2 = 0; while(string1 [count1] !=''\''') { if(islower((int)string1 [count1])) { string2 [count2] = string1 [count1]; count1 ++; count2 ++; } else { count1 ++; } } string2 [count2 + 1] =''\'''; printf(" isLower String2 =%s \ n",string2); return; } 当我编译时,gcc -Wall给了我这个错误,即使我认为 我正在返回string2。 copy_elements.c:在函数`isLowercase''中: copy_elements.c:51:警告:`return''没有值,在函数中 返回非虚空 我试过返回string2;但是我不明白gcc给出的警告: cp_elements.c:在函数`isLowercase'': cp_elements.c:51:警告:return使整数来自指针没有 施放 所以我尝试输入cast我的返回字符串,但这没有帮助。 我试过运行程序,得到以下结果。 请输入一个字符串进行检查:QwerT12 isLower String2 = wer Main String1 = QwerT12 虽然它确实找到了正确的小写字母,isLower String2 = wer,但似乎字符串没有被填充返回主页。 为什么我在Main中看不到结果,即Main String2在什么? 谢谢。 Pat 解决方案 pt*****@gmail.com 说: < ; snip> char isLowercase(char * string1,char * string2) { < snip> return; } 当我编译,gcc -Wall给了我这个错误,即使我认为 我正在返回string2。 你为什么这么认为? copy_elements.c:在函数`isLowercase'': copy_elements.c:51:警告:返回没有任何价值,在功能上 返回非虚空 我尝试返回string2; 好的。 但我不明白gcc给出的警告: cp_elements.c:在函数`isLowercase''中: cp_elements.c:51:警告:返回从指针生成整数而没有 cast char与char *不同。 > 所以我尝试输入我的返回字符串,但这没有帮助。 投射几乎总是错误的解决方案。这个 案例中的正确解决方案是使函数的返回类型与事物的类型匹配 你希望从它返回 - char *,而不是char。 - Richard Heathfield Usenet是一个奇怪的地方 - dmr 29/7/1999 http://www.cpax.org.uk 电子邮件:rjh在上述域名中, - www。 在文章< 11 ******* **************@o5g2000hsb.googlegroups.c om>, < pt ***** @ gmail.comwrote: [...] string2 [count2 + 1] =''\'''; printf(" isLower String2 =%s \ n",string2); return; } >当我编译时,gcc -Wall给了我这个错误,即使我认为我正在返回string2。 copy_elements.c:在函数`isLowercase'': copy_elements.c:51:警告:`return''没有值,在函数中返回non-void 你没有返回string2 。你有返回;在 函数的末尾;要返回string2,你需要return string2;。 >我试过返回string2;但我不明白gcc给出的警告: cp_elements.c:在函数`isLowercase'': cp_elements.c:51:警告:返回从指针生成整数而没有 cast string2是一个char *。 isLowercase声明如下: > char isLowercase(char * string1,char * string2) 表示它返回一个char(这是一种整数),而不是a char *。将声明更改为char * isLowercase(... 顺便说一下,isLowercase对于 的函数来说不是一个好名字这样做: > / *将小写元素从string1复制到string2。* / - - 理查德 - 考虑在一些字母表中需要多达32个字符 - X3。 4,1963。 pt ***** @ gmail .com D'μà£o 尝试使用此代码来帮助我理解字符串和函数/> 但我不确定为什么会出现这些错误,希望有人可以给我的学习带来一些启示。 # include< stdio.h> #include< string.h> #include< ctype.h> #define STRINGSIZE 1 0 char isLowercase(char * string,char * string2); int main() { char string1 [STRINGSIZE]; char string2 [STRINGSIZE]; printf("请输入一个字符串进行检查:"); fgets(string1,STRINGSIZE,stdin); isLowercase(string1,string2); printf(" Main String1 =%s",string1); printf(" Main String2 =%s",string2); 返回0; } char isLowercase(char * string1,char * string2) { / *将小写元素从string1复制到string2。 * / int count1 = 0; int count2 = 0; while(string1 [count1] !=''\''') { if(islower((int)string1 [count1])) { string2 [count2] = string1 [count1]; count1 ++; count2 ++; } else { count1 ++; } } string2 [count2 + 1] =''\'''; printf(" isLower String2 =%s \ n",string2); return; } 当我编译时,gcc -Wall给了我这个错误,即使我认为 我是返回string2。 copy_elements.c:在函数`isLowercase'': copy_elements.c:51:警告:`return''没有值,在函数中 返回非虚空 我试过返回string2;但是我不明白gcc给出的警告: cp_elements.c:在函数`isLowercase'': cp_elements.c:51:警告:return使整数来自指针没有 施放 所以我尝试输入cast我的返回字符串,但这没有帮助。 我试过运行程序,得到以下结果。 请输入一个字符串进行检查:QwerT12 isLower String2 = wer Main String1 = QwerT12 虽然它确实找到了正确的小写字母,isLower String2 = wer,但似乎字符串没有被填充返回主页。 为什么我在Main中看不到结果,即Main String2在什么? 谢谢。 Pat 1. char是一个小整数 2.字符串是编译器视图中char的指针 3. Fuction isLowercase返回一个char,即返回一个整数。 如果你返回string2,你会返回一个指针 这就是她给你警告的原因; cp_elements.c:在函数`isLowercase'中: cp_elements.c:51:警告:返回从指针生成整数 没有投射 4.Main String2被淘汰了。它可能是IO 缓冲区的影响。输出将首先存储在缓冲区中,当缓冲区中有足够的字符或某些事件发生时,将输出('\\'n''被放入,例如), 内容将刷新到屏幕。 解决方案1: 只需修改声明: printf(" Main String2 =%s",string2); to printf(" Main String2 =%s \ n" ;,string2); 你会看到它。 解决方案2: 添加语句 printf(" Main String2 =%s",string2); getchar(); / BLOCKQUOTE> Hi,Tried this code to assist my understanding of strings and functionsbut I''m not sure why the errors are occurring and hope someone canshed some light to my learning.#include <stdio.h>#include <string.h>#include <ctype.h>#define STRINGSIZE 10char isLowercase(char *string, char *string2);int main(){char string1[STRINGSIZE];char string2[STRINGSIZE];printf("Please enter a string for checking: ");fgets(string1, STRINGSIZE, stdin);isLowercase(string1, string2);printf("Main String1 = %s",string1);printf("Main String2 = %s",string2);return 0;}char isLowercase(char *string1, char *string2){/* copies lowercase elements from string1 to string2. */int count1 = 0;int count2 = 0;while (string1[count1] != ''\0''){if (islower((int) string1[count1])){string2[count2] = string1[count1];count1++;count2++;}else{count1++;}}string2[count2 + 1] = ''\0'';printf("isLower String2 = %s\n",string2);return;}When I compile, gcc -Wall gave me this error, even though I thoughtthat I''m returning string2.copy_elements.c: In function `isLowercase'':copy_elements.c:51: warning: `return'' with no value, in functionreturning non-voidI tried return string2; but I don''t understand the warning gcc gives:cp_elements.c: In function `isLowercase'':cp_elements.c:51: warning: return makes integer from pointer without acastSo I tried type cast my return string but this didn''t help.I tried running the program and I get the following result.Please enter a string for checking: QwerT12isLower String2 = werMain String1 = QwerT12While it does find the correct lower case letters, isLower String2 =wer, it would appear that the string is not being pased back to main.Why I don''t see a result back in Main, ie "Main String2" at all ?Thank you.Pat 解决方案 pt*****@gmail.com said:<snip>char isLowercase(char *string1, char *string2){<snip> return;}When I compile, gcc -Wall gave me this error, even though I thoughtthat I''m returning string2.Why did you think that?copy_elements.c: In function `isLowercase'':copy_elements.c:51: warning: `return'' with no value, in functionreturning non-voidI tried return string2;Okay.but I don''t understand the warning gcc gives:cp_elements.c: In function `isLowercase'':cp_elements.c:51: warning: return makes integer from pointer without acastchar isn''t the same as char *.>So I tried type cast my return string but this didn''t help.Casting is almost always the wrong solution. The right solution in thiscase is to make the function''s return type match the type of the thingyou wish to return from it - char *, rather than char.--Richard Heathfield"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.ukemail: rjh at the above domain, - www.In article <11*********************@o5g2000hsb.googlegroups.c om>,<pt*****@gmail.comwrote:[...] string2[count2 + 1] = ''\0''; printf("isLower String2 = %s\n",string2); return;}>When I compile, gcc -Wall gave me this error, even though I thoughtthat I''m returning string2.copy_elements.c: In function `isLowercase'':copy_elements.c:51: warning: `return'' with no value, in functionreturning non-voidYou aren''t returning string2. You have "return;" at the end of thefunction; to return string2 you would need "return string2;".>I tried return string2; but I don''t understand the warning gcc gives:cp_elements.c: In function `isLowercase'':cp_elements.c:51: warning: return makes integer from pointer without acaststring2 is a char *. isLowercase is declared like this:>char isLowercase(char *string1, char *string2)which says that it returns a char (which is a kind of integer), nota char *. Change the declaration to "char *isLowercase(..."Incidentally, "isLowercase" is not a good name for a function thatdoes this:>/* copies lowercase elements from string1 to string2. */-- Richard--"Consideration shall be given to the need for as many as 32 charactersin some alphabets" - X3.4, 1963. pt*****@gmail.com D′μà£oHi,Tried this code to assist my understanding of strings and functionsbut I''m not sure why the errors are occurring and hope someone canshed some light to my learning.#include <stdio.h>#include <string.h>#include <ctype.h>#define STRINGSIZE 10char isLowercase(char *string, char *string2);int main(){ char string1[STRINGSIZE]; char string2[STRINGSIZE]; printf("Please enter a string for checking: "); fgets(string1, STRINGSIZE, stdin); isLowercase(string1, string2); printf("Main String1 = %s",string1); printf("Main String2 = %s",string2); return 0;}char isLowercase(char *string1, char *string2){/* copies lowercase elements from string1 to string2. */ int count1 = 0; int count2 = 0; while (string1[count1] != ''\0'') { if (islower((int) string1[count1])) { string2[count2] = string1[count1]; count1++; count2++; } else { count1++; } } string2[count2 + 1] = ''\0''; printf("isLower String2 = %s\n",string2); return;}When I compile, gcc -Wall gave me this error, even though I thoughtthat I''m returning string2.copy_elements.c: In function `isLowercase'':copy_elements.c:51: warning: `return'' with no value, in functionreturning non-voidI tried return string2; but I don''t understand the warning gcc gives:cp_elements.c: In function `isLowercase'':cp_elements.c:51: warning: return makes integer from pointer without acastSo I tried type cast my return string but this didn''t help.I tried running the program and I get the following result.Please enter a string for checking: QwerT12isLower String2 = werMain String1 = QwerT12While it does find the correct lower case letters, isLower String2 =wer, it would appear that the string is not being pased back to main.Why I don''t see a result back in Main, ie "Main String2" at all ?Thank you.Pat1. char is a small integer2. string is a pointer of char in compiler''s view3. Fuction isLowercase return a char,that is it returns an integer.If you return string2, you return a pointerthat is why she gives you the warning;cp_elements.c: In function `isLowercase'':cp_elements.c:51: warning: return makes integer from pointerwithout a cast4.The "Main String2" is outputed. It maybe a effect of the IObuffer. The output will be first stored in a buffer,when there is enough characters in the buffer or some eventhappens ( a ''\n'' is put in, for example),the contents will be flushed to screen.Solution 1:Just modify the statement :printf("Main String2 = %s",string2);toprintf("Main String2 = %s\n",string2);And you will see it .Solution 2:Add statementgetchar();afterprintf("Main String2 = %s",string2); 这篇关于在函数之间传递字符串问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-14 21:02