C语言 带字符数组参数的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23602039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Function with char array parameter
提问by Kamil
I'm trying to write a function for printing text in C for PIC microcontrollers (I think it's based on gcc).
我正在尝试为 PIC 微控制器编写一个用 C 打印文本的函数(我认为它基于 gcc)。
void print(char[] text);
void print(char[] text)
{
for(ch = 0; ch < sizeof(text); ch ++)
{
do_something_with_character(text[ch]);
}
}
and call it like this:
并这样称呼它:
print("some text");
I'm getting compiler complaints about wrong brackets.
我收到有关错误括号的编译器投诉。
What is wrong with this?
How can I use char array pointer in this case?
这有什么问题?
在这种情况下如何使用 char 数组指针?
回答by Jonathon Reinhart
As mentioned in other answers, your syntax is incorrect. The brackets belong aftertext.
正如其他答案中所述,您的语法不正确。括号属于后text。
Also of significant importance, sizeofdoes notdo what you're expecting here:
同样重要的是,sizeof它不会做你在这里期望的事情:
void print(char[] text)
{
for(ch = 0; ch < sizeof(text); ch ++)
^^^^^^
Remember, sizeofis a compile-timeoperator - the compiler replaces it with the size while the code is being built. It can't possibly know the size at runtime.
请记住,sizeof是一个编译时运算符 - 编译器在构建代码时将其替换为大小。它不可能知道运行时的大小。
In this case, sizeof(text)is always going to return sizeof(void*), or 4 on most 32-bit systems. Your platform may vary.
在这种情况下,在大多数 32 位系统上sizeof(text)总是会返回sizeof(void*), 或 4 。您的平台可能会有所不同。
You have two options:
您有两个选择:
- Pass the length in another parameter to print.
- Treat the
char[]as a "C string", where the length is unknown, but the string is terminated by NUL character.
- 在另一个参数中传递长度以进行打印。
- 将
char[]视为"C string",其中长度未知,但字符串以 NUL 字符结尾。
The latter is your best bet, and you should probably replace char[]with char*.
后者是你最好的选择,你可能应该char[]用char*.
Here we use a NUL-terminated string, and iterate over it with a pointer:
这里我们使用一个以 NUL 结尾的字符串,并用一个指针遍历它:
void print(const char* text)
{
const char* p;
// Iterate over each character of `text`,
// until we reach the NUL terminator
for (p = text; *p != 'void print(char text[])
'; p++) {
// Pass each character to some function
do_something_with_character(*p);
}
}
回答by Lee Duhem
The correct syntax is
正确的语法是
void print(char *text)
or
或者
void print(char *text);
void print(char *text)
{
while(*text)
{
do_something_with_character(*text);
++text;
}
}
In print(), you cannot use sizeofoperator to find out the length of string text, you need to use strlen()(include <string.h>first) or test whether text[ch]is \0.
在 中print(),您不能使用sizeof运算符来找出 string 的长度text,您需要使用strlen()(include <string.h>first) 或测试是否text[ch]为\0。
回答by Mahonri Moriancumer
I would do it like this:
我会这样做:
void print(char[] text);
void print(char[] text)
回答by Jonathan Leffler
You have to place the square brackets in the correct place:
您必须将方括号放在正确的位置:
void print(char[] text);
void print(char[] text)
void print(char text[]);
void print(char text[])
void print(char *text);
void print(char *text)
or use pointer notation:
或使用指针符号:
for(ch = 0; ch < sizeof(text); ch ++)
Also, note that even if you use the array notation, the parameter is effectively rewritten to a pointer, and then the code in the body of the function:
另外,请注意,即使您使用数组表示法,参数也被有效地重写为指针,然后是函数体中的代码:
size_t len = strlen(text);
for (size_t ch = 0; ch < len; ch++)
is wrong. You almost certainly don't want the 4 or 8 bytes that is the size of the pointer. You probably want:
是错的。您几乎肯定不想要指针大小的 4 或 8 个字节。你可能想要:
void print(char *text);
^
If you can't declare variables in your loop (C99 or later required), declare it separately. You didn't show the declaration of chin your code. If it compiled, that means chis a global variable — which is pretty horrid.
如果您不能在循环中声明变量(需要 C99 或更高版本),请单独声明。您没有ch在代码中显示的声明。如果它被编译,那意味着它ch是一个全局变量——这是非常可怕的。
Note that the ++operator binds tightly and should not be separated from its operand by spaces.
请注意,++运算符绑定紧密,不应用空格将其与其操作数分开。
回答by masoud
Pass C-style string by pointers, or brackets after variable name:
通过指针或变量名后的括号传递 C 风格的字符串:
void print(char text[]);
^^
or
或者
int len = strlen(text);
for(ch = 0; ch < len; ch ++)
^^^^
To calculate the length of a string, use strlennot sizeof.
要计算字符串的长度,请使用strlennot sizeof。
// empty character array
// cannot be changed or given a value
char text[];
// character array initialized to the length of its content
char text[] = "This is a string";
// character array with length 1000
// the elements may be individually manipulated using indices
char text[1000];
回答by samrap
Judging on your question and the observation that you are probably a beginner in C, I'll attempt to answer your question without using pointers like the other answers here.
根据您的问题以及您可能是 C 初学者的观察,我将尝试回答您的问题,而不像这里的其他答案一样使用指针。
First off, Jonathon Reinhart makes an excellent point, that sizeofis not the proper usage here. Also, as others pointed out, the correct syntax for an array of characters, which is what you are using in your code, is as follows:
首先,乔纳森莱因哈特提出了一个很好的观点,这sizeof不是这里的正确用法。此外,正如其他人所指出的,字符数组的正确语法(即您在代码中使用的)如下:
#include <string.h>
void print(char text[]);
int main()
{
char text[] = "This is a string";
print(text);
return 0
}
void print(char text[])
{
// ALWAYS define a type for your variables
int ch, len = strlen(text);
for(ch = 0; ch < len; ch++) {
do_something_with_character(text[ch]);
}
}
In your case, I would do something like this:
在你的情况下,我会做这样的事情:
##代码##The standard library header string.hprovides the strlenfunction which returns an integer value (actually an unsigned long) of the length of the string excludingthe terminating NULL character \0. In C, strings are just arrays of characters and the way you specify the end of a string is by including \0at the end.
标准库头文件string.h提供了一个strlen函数,该函数返回字符串长度的整数值(实际上是一个无符号长整型),不包括终止的 NULL 字符\0。在 C 中,字符串只是字符数组,指定字符串结尾的方式是\0在结尾处包含。

