C 指向字符的指针

标签 c pointers char

我对 C 指针有一些误解:

void putString(char* StringPtr, int length){
  for(int i=0; i< length; i++)       
  {                   
    USART_send(*StringPtr);      
    StringPtr++;
  }
}

void parseMsg(char* in_string, int str_len) {
  int i = 0;
  putString(in_string, str_len);
  for(i = 0; i <= str_len; i++) 
  {
    char* temp_pt = &in_string[i];
    putString(temp_pt, 1);
  }
}

int main(int arg) {
  char* myChar = "abcdefg";
  parseMsg(myChar, 7);
}

编辑:parseMsg 中,当我调用第一个 putString 时,效果很好。当我尝试循环打印每一个时,它没有。 USART_send 只是将字符输出到我的终端。

最佳答案

那是因为 test[i]char 类型而不是 char * 类型。

您可以分配给 char:

 char temp = test[i];

或者获取它的地址:

 char *temp = &test[i];

关于C 指向字符的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15721859/

相关文章:

dec 中的循环变换 (C)

c - 如何使用 Pthreads 并行化我的 n 皇后拼图?

c++ - 我收到一个看起来应该是 "just work."的程序的奇怪错误

分配 `ab` 时包含单个字符的字符

c - 将数组 char 转换为数组 Short 时的奇怪行为

c - 在c中将文本文件读入字符数组

c - qsort 无法正确排序 C 中结构体的指针数组

c++ - 为什么通过指针交换在 C++ 中的 vector 中不起作用?

c - 如何将结构体转换为字符数组?

c - C语言中如何判断一个字符数组有多少个字符?