通过函数传递字符串(C 编程)
我刚刚开始学习指针,经过多次添加和删除 *
后,我将输入的字符串转换为大写的代码终于起作用了。
#include <stdio.h>
char* upper(char *word);
int main()
{
char word[100];
printf("Enter a string: ");
gets(word);
printf("\nThe uppercase equivalent is: %s\n",upper(word));
return 0;
}
char* upper(char *word)
{
int i;
for (i=0;i<strlen(word);i++) word[i]=(word[i]>96&&word[i]<123)?word[i]-32:word[i];
return word;
}
我的问题是,在调用我发送的函数时 word< /code> 本身就是一个指针,那么在
char* upper(char *word)
中为什么我需要使用 *word
?
它是一个指向指针的指针吗?另外,那里是否有一个 char*
因为它返回一个指向字符/字符串的指针?
请向我解释一下这是如何工作的。
I have just started learning pointers, and after much adding and removing *
s my code for converting an entered string to uppercase finally works..
#include <stdio.h>
char* upper(char *word);
int main()
{
char word[100];
printf("Enter a string: ");
gets(word);
printf("\nThe uppercase equivalent is: %s\n",upper(word));
return 0;
}
char* upper(char *word)
{
int i;
for (i=0;i<strlen(word);i++) word[i]=(word[i]>96&&word[i]<123)?word[i]-32:word[i];
return word;
}
My question is, while calling the function I sent word
which is a pointer itself, so in char* upper(char *word)
why do I need to use *word
?
Is it a pointer to a pointer? Also, is there a char*
there because it returns a pointer to a character/string right?
Please clarify me regarding how this works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为这里需要的类型只是“指向char的指针”,表示为
char *
,星号(*)是参数类型规范的一部分。它不是“指向 char 的指针”,它会写成char **
一些附加说明:
您似乎混淆了取消引用运算符 < code>*(用于访问指针指向的地方),以星号作为类型规范中的指针符号;您没有在代码中的任何地方使用取消引用运算符;您仅使用星号作为类型规范的一部分!请参阅这些示例:要将变量声明为指向 char 的指针,您可以编写:
要将值分配给
a
所指向的空间(通过使用取消引用运算符),您可以编写:<前><代码>*a = 'c';
(char 的)数组并不完全等于(指向 char 的)指针(另请参见 这里的问题)。但是,在大多数情况下,(char)数组可以转换为(char)指针。
您的函数实际上更改了外部 char 数组(并且传回指向它的指针); printf 不仅会打印出输入内容的大写字母,而且
main
函数的变量word
也会被修改,使其保存输入单词的大写字母。请小心,这样的副作用实际上正是您想要的。如果您不希望函数能够修改外部变量,您可以编写char* upper(char const *word)
- 但随后您还必须更改函数定义,这样它就不会直接修改word
变量,否则编译器会抱怨。That's because the type you need here simply is "pointer to char", which is denoted as
char *
, the asterisk (*) is part of the type specification of the parameter. It's not a "pointer to pointer to char", that would be written aschar **
Some additional remarks:
It seems you're confusing the dereference operator
*
(used to access the place where a pointer points to) with the asterisk as a pointer sign in type specifcations; you're not using a dereference operator anywhere in your code; you're only using the asterisk as part of the type specification! See these examples: to declare variable as a pointer to char, you'd write:To assign a value to the space where
a
is pointing to (by using the dereference operator), you'd write:An array (of char) is not exactly equal to a pointer (to char) (see also the question here). However, in most cases, an array (of char) can be converted to a (char) pointer.
Your function actually changes the outer char array (and passes back a pointer to it); not only will the uppercase of what was entered be printed by printf, but also the variable
word
of themain
function will be modified so that it holds the uppercase of the entered word. Take good care the such a side-effect is actually what you want. If you don't want the function to be able to modify the outside variable, you could writechar* upper(char const *word)
- but then you'd have to change your function definition as well, so that it doesn't directly modify theword
variable, otherwise the Compiler will complain.char upper(char c)
是一个接受一个字符并返回一个字符的函数。如果您想使用字符串,则约定是字符串是以空字符结尾的字符序列。您无法将完整的字符串传递给函数,因此您将指针传递给第一个字符,因此char *upper(char *s)
。指向指针的指针将有两个*
,如char **pp
:upper 也可以实现为
void upper(char *str)
,但让 upper 返回传递的字符串更方便。当您printf
upper 返回的字符串时,您在示例中使用了它。就像评论一样,可以优化你的上层功能。您正在为每个
i
调用strlen
。 C 字符串始终以 null 结尾,因此您可以替换i
i
i
i
i
i
i < strlen(word)
与word[i] != '\0'
(或word[i] != 0
)。另外,如果您不比较 96 和 123 并减去 32,但如果您使用“a”、“z”、“A”、“Z”或您想到的任何字符进行检查和计算,则代码会更好读。char upper(char c)
would be a function that takes a character and returns a character. If you want to work with strings the convention is that strings are a sequence of characters terminated by a null character. You cannot pass the complete string to a function so you pass the pointer to the first character, thereforechar *upper(char *s)
. A pointer to a pointer would have two*
like inchar **pp
:upper could also be implemented as
void upper(char *str)
, but it is more convenient to have upper return the passed string. You made use of that in your sample when youprintf
the string that is returned by upper.Just as a comment, you can optimize your upper function. You are calling
strlen
for everyi
. C strings are always null terminated, so you can replace youri < strlen(word)
withword[i] != '\0'
(orword[i] != 0
). Also the code is better to read if you do not compare against 96 and 123 and subtract 32 but if you check against and calculate with 'a', 'z', 'A', 'Z' or whatever character you have in mind.*words 是即使函数中的数组字的指针和指针字实际上指向同一个事物,同时传递参数 jst 的“被指向者”的副本,即传递输入的单词并完成任何操作是在指针字上完成的,所以最后我们必须返回一个指针,因此返回类型被指定为*。
the *words is even though a pointer bt the array word in function and the pointer word are actually pointing to the one and the same thing while passing arguments jst a copy of the "pointee" ie the word entered is passed and whatever operation is done is done on the pointer word so in the end we have to return a pointer so the return type is specified as *.