【问题标题】:Function with char array parameter带有 char 数组参数的函数
【发布时间】:2014-06-29 09:40:07
【问题描述】:

我正在尝试为 PIC 微控制器编写一个用 C 语言打印文本的函数(我认为它基于 gcc)。

void print(char[] text);

void print(char[] text)
{
    for(ch = 0; ch < sizeof(text); ch ++)
    {
        do_something_with_character(text[ch]);
    }
}

然后这样称呼它:

print("some text");

我收到关于错误括号的编译器投诉。

  1. 这是怎么回事?

  2. 这种情况下如何使用char数组指针?

【问题讨论】:

标签: c arrays pointers parameter-passing


【解决方案1】:

通过指针或变量名后的括号传递 C 风格的字符串:

void print(char *text);
                ^

void print(char text[]);
                    ^^

要计算字符串的长度,请使用strlen 而不是sizeof

int len = strlen(text);
for(ch = 0; ch < len; ch ++)
                 ^^^^

【讨论】:

  • 请注意,在循环条件中使用 strlen() 会将线性算法转换为二次算法,因为每次迭代都会评估长度,除非编译器非常聪明并且可以确定没有任何东西会改变字符串。
  • @JonathanLeffler:好点,已编辑。我来自 C++ 领域,在循环中调用 string::length() 还不错。
【解决方案2】:

正如其他答案中提到的,您的语法不正确。括号属于之后 text

同样重要的是,sizeof没有按照您的期望行事:

void print(char[] text)
{
    for(ch = 0; ch < sizeof(text); ch ++)
                     ^^^^^^

请记住,sizeof 是一个 compile-time 运算符 - 编译器在构建代码时将其替换为大小。它不可能在运行时知道大小。

在这种情况下,sizeof(text) 总是会返回 sizeof(void*),或者在大多数 32 位系统上为 4。您的平台可能会有所不同。


你有两个选择:

  • 在另一个参数中传递长度以进行打印。
  • char[] 视为"C string",其中长度未知,但字符串以NUL 字符结尾。

后者是您最好的选择,您应该将char[] 替换为char*

这里我们使用一个以 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 != '\0'; p++) {

        // Pass each character to some function
        do_something_with_character(*p);
    }
}

【讨论】:

  • 如何动态测量该字符数组的大小?
  • @Kamil 请参阅strlen。但请注意:在处理典型的 C 字符串时,您很少关心字符串的实际长度。
【解决方案3】:

你必须把方括号放在正确的位置:

void print(char[] text);

void print(char[] text)

void print(char text[]);

void print(char text[])

或使用指针表示法:

void print(char *text);

void print(char *text)

另外,请注意,即使使用数组表示法,参数也会被有效地重写为指针,然后是函数体中的代码:

for(ch = 0; ch < sizeof(text); ch ++)

错了。您几乎可以肯定不想要指针大小的 4 或 8 个字节。你可能想要:

size_t len = strlen(text);
for (size_t ch = 0; ch < len; ch++)

如果您无法在循环中声明变量(需要 C99 或更高版本),请单独声明它。您没有在代码中显示 ch 的声明。如果它编译了,这意味着ch 是一个全局变量——这非常可怕。

请注意,++ 运算符绑定紧密,不应使用空格与其操作数分隔。

【讨论】:

  • 留下描述性解释有什么意义?请给我样品代码。
  • '这就是世界的方式。对此感到不安是没有意义的,尽管从大多数角度来看,这是合理的。
【解决方案4】:

正确的语法是

void print(char text[])

void print(char *text)

print()中,不能使用sizeof运算符来查找字符串text的长度,需要使用strlen()(先包含&lt;string.h&gt;)或者测试text[ch]是否为\0 .

【讨论】:

    【解决方案5】:

    我会这样做:

    void print(char *text);
    
    void print(char *text)
       {
       while(*text)
          {
          do_something_with_character(*text);
          ++text;
          }
       }
    

    【讨论】:

      【解决方案6】:

      根据您的问题以及您可能是 C 初学者的观察,我将尝试在不使用此处其他答案的指针的情况下回答您的问题。

      首先,Jonathon Reinhart 提出了一个很好的观点,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];
      

      在你的情况下,我会这样做:

      #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]);
          }
      }
      

      标准库头文件string.h 提供了strlen 函数,它返回字符串长度的整数值(实际上是一个无符号长整数)不包括终止NULL 字符\0。在 C 中,字符串只是字符数组,指定字符串结尾的方式是在结尾包含 \0

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-10-21
        • 2017-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-27
        • 1970-01-01
        相关资源
        最近更新 更多