Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有char数组参数的函数_C_Arrays_Pointers_Parameter Passing - Fatal编程技术网

具有char数组参数的函数

具有char数组参数的函数,c,arrays,pointers,parameter-passing,C,Arrays,Pointers,Parameter Passing,我正在尝试为PIC微控制器编写一个用C语言打印文本的函数,我认为它是基于gcc的 void print(char[] text); void print(char[] text) { for(ch = 0; ch < sizeof(text); ch ++) { do_something_with_character(text[ch]); } } 我收到编译器关于错误括号的投诉 这有什么问题 在这种情况下,如何使用字符数组指针 通过指针或变量名后

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

void print(char[] text);

void print(char[] text)
{
    for(ch = 0; ch < sizeof(text); ch ++)
    {
        do_something_with_character(text[ch]);
    }
}
我收到编译器关于错误括号的投诉

这有什么问题

在这种情况下,如何使用字符数组指针


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

void print(char *text);
                ^

要计算字符串的长度,请使用strlen not sizeof


如其他答案中所述,您的语法不正确。括号位于文本之后

同样重要的是,没有做到您在这里期望的:

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

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

或使用指针表示法:

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是一个全局变量——这非常可怕

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

正确的语法是

void print(char text[])


在打印中,您不能使用sizeof运算符来确定字符串文本的长度,您需要先使用strlen include或测试text[ch]是否为\0。

我会这样做:

print("some text");
void print(char *text);

void print(char *text)
   {
   while(*text)
      {
      do_something_with_character(*text);
      ++text;
      }
   }
#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]);
    }
}

根据您的问题和您可能是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];
在你的情况下,我会这样做:

print("some text");
void print(char *text);

void print(char *text)
   {
   while(*text)
      {
      do_something_with_character(*text);
      ++text;
      }
   }
#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]);
    }
}

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

如何动态测量该字符数组的大小?@Kamil See。但请注意:在处理典型的C字符串时,您很少关心字符串的实际长度。实际char[]文本的可能重复不是有效的C语法。它应该是char text[]或char*text注意,在循环条件中使用strlen会将线性算法转换为二次算法,因为每次迭代都会计算长度,除非编译器非常聪明,并且能够确定字符串的长度不会发生任何变化。@JonathanLeffler:好的,已编辑。我来自C++领域,它调用String::循环中的长度不是很坏,留下描述性解释的意义是什么?只要给我一个简单的密码,这就是世界的方式。对此感到不安是没有意义的,尽管从大多数观点来看,这是有道理的。
void print(char *text);

void print(char *text)
   {
   while(*text)
      {
      do_something_with_character(*text);
      ++text;
      }
   }
// 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]);
    }
}