【问题标题】:How to access char[] from char* in c?如何在 c 中从 char* 访问 char[]?
【发布时间】:2017-09-19 02:39:33
【问题描述】:

我正在用 C 语言编写 Arduino 板,因此除非我与外部终端窗口进行串行通信,否则无法打印。

因此我开发了一个 printAll 方法:

void printAll(char * str) {
  int i;
  for(i = 0; str[i] != 0x00; i++) {
    PSerial_write('0', str[i]);
  }
}

我还有一个变量:

char input[12];
input[0] = 'h';
input[1] = 'e';
input[2] = 'l';
input[3] = 'p';

我想做的是将此数组传递给 printAll 方法(但 printAll 方法需要一个 char*)。

我已经尝试过:

printAll(&input[0]);

但是什么都没有打印出来!但是当我逐步打印输入数组的每个字符时,我得到:

help<0><0><0><0><0><0><0><0>

谁能解释为什么这不起作用?谢谢!

***注意:当这样使用时,printAll 方法工作得非常好:

printAll("Hello World!");

总的来说,我的代码如下所示:

char input[12];

int main(void) {
  start();
}

void start() {
  while(1) {
    printAll("Please enter in a command!\r");
    printAll("Please type 'help' for instructions!\r");
    char input[12];
    readInput(input);
    printAll("Trying to print the char array stuff....\r");
    printAll(input);
    if (input == "help") printHelp();
    else if (input == "set") {
      if (setStatus) printSet();
      else printAll("Controller not authorized to print.\n");
    }
    else if (input == "set on") setStatus = true;
    else if (input == "set off") setStatus = false;
    else if (input == "set hex=on") hexFormat = true;
    else if (input == "set hex=off") hexFormat = false;
    else if (input == "set tlow") tlow = getNumber(input);
    else if (input == "set thigh") thigh = getNumber(input);
    else if (input == "set period") period = getNumber(input);
    x_yield();
  }
}

void readInput() {
  char c = PSerial_read('0'); //reads character from user
  while (c != '\r') {
    //while the character isnt 'enter'
    input[currIndex] = c;
    c = PSerial_read('0');
    currIndex++;
  }
  int y;
  for(y = 0; y < 12; y++) {
    PSerial_write('0', input[y]);
    //go through input and print each character
  }
  PSerial_write('0', '\r');
  //add new line to print log
  currIndex = 0; //reset for next input (overwrites current stuff)
}

现在无论我输入什么,它只是要求更多的输入,并且在输入法返回后从不打印数组。

【问题讨论】:

  • 在打印之前也做input[4] = 0;

标签: c arrays pointers arduino char


【解决方案1】:

您发送的代码是混合的,无法编译。该代码建议您有两个输入变量,一个是全局变量,一个是主变量。 readInput() 读取全局变量, printAll() 读取本地变量(反之亦然,具体取决于更改的代码)。删除无论如何都不应该使用的全局输入,并将相同的输入变量传递给 readInput() 和 printAll()。

【讨论】:

  • 是的,会的。 idk 我怎么错过了。谢谢你的好眼睛!
  • 虽然我有你...它正确打印我输入的内容,但如果它与 if 语句块中的关键字匹配,它不会执行它们。我没有使用正确的比较来检查输入和那些字符串文字之间的相等性吗? JK 我只是快速搜索了一下,发现 strcmp 可以完成这项工作
  • if (strncmp(input, "help", sizeof(input)) == 0) printHelp();
  • 使用“-Wall -Wextra”编译以查看更多警告
【解决方案2】:

注意字符串文字“Hello World!”是一个字符[]。它正确打印的事实意味着您的代码有其他问题。请发布您尝试运行的整个代码。

另外,请注意 char[] 在引用时会自动转换为 char *。这称为数组衰减为指针。见:

Why do arrays in C decay to pointers?

所以,两者:

printAll(&input[0]);

printAll(input);

做同样的事情:他们将输入转换为 char *。

编辑:从您的较大代码中,我至少可以看到您在阅读后没有添加“\0”字符,并且在打印时没有发送“\r”。字符串比较也是不正确的,应该用 strcmp() 来完成。

【讨论】:

  • 对其进行了编辑。这就是我到目前为止所拥有的。我确实有 if/else if 语句使用的方法,但代码没有那么远,所以我认为没有必要发布它们
  • 我添加了一些 cmets,但我建议从一个干净的代码开始,只定义数组并打印它。
【解决方案3】:

我对 Arduinos 了解不多,所以请耐心等待。

在您的readInput 函数中,您没有为input 数组包含任何参数,这意味着您在此函数中使用了在代码开头声明的全局变量。但是,在您的 start 函数中,您声明另一个 input 数组并将其传递给 readInput,即使该函数不接受任何参数。

这个新的input 实际上将“隐藏”全局变量input 用于声明它的函数。这意味着start 使用这个局部input,即使readInput 使用全局input .换句话说,您实际上使用了两个不同的变量,而不是一个。

要解决此问题,请尝试删除 start 中的 char input[12],或删除代码开头的全局 char input[12] 并将其添加为 readInput 的参数:void readInput(char * input)

注意:即使您没有在readInput 中包含任何参数,C 编译器也不会在传递参数时报错,除非您将void 放在括号中:void readInput(void) .

【讨论】:

  • 我对 c 编译器一无所知!将来会有用,谢谢!
  • 虽然我有你...它正确打印我输入的内容,但如果它与 if 语句块中的关键字匹配,它不会执行它们。我没有使用正确的比较来检查输入和那些字符串文字之间的相等性吗? JK 我只是快速搜索了一下,发现 strcmp 可以完成这项工作
  • 比较字符串时,必须使用strcmp()函数。使用== 运算符会将数组视为指针。
猜你喜欢
  • 1970-01-01
  • 2019-07-30
  • 2014-11-04
  • 2011-01-05
  • 2012-02-16
  • 2021-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多