【问题标题】:Compiler errors calling sprintf: "expected 'char *' but argument is of type 'char'"调用 sprintf 的编译器错误:“预期为 'char *',但参数的类型为 'char'”
【发布时间】:2016-11-01 17:55:45
【问题描述】:

我正在尝试用 C 语言对微芯片进行编程。现在我正在努力更新 LCD 屏幕上的一行,但它无法正常工作。任何人都可以对此有所了解吗?

float slope = 0.0626;
char *mystring;
int8_t    LCDline1[20];

void myfunction(){
    sprintf(*mystring, "%ld", (long)(slope * 10000));
    memcpy(LCDline1, *mystring, strlen(*mystring)+1);
}

当我运行编译代码时,出现以下三个错误。

calibrate.c:60:5: warning: passing argument 1 of 'sprintf' makes
pointer from integer without a cast. note: expected 'char *'
but argument is of type 'char'

calibrate.c:61:5: warning: passing argument 1 of 'strlen' makes
pointer from integer without a cast.  note: expected 'const char *'
but argument is of type 'char'

calibrate.c:61:5: warning: passing argument 2 of 'memcpy' makes
pointer from integer without a cast. note: expected 'const void *' but
argument is of type 'char'

我不确定我做错了什么,我使用以下定义作为我的起点

void *memcpy(void *str1, const void *str2, size_t n)
size_t strlen(const char *str)
char *p = "String";

【问题讨论】:

    标签: c arrays pointers memcpy


    【解决方案1】:
    Char *c;
    

    这可以是存储字符的数组的起始地址,但是,

    c //is the pointer to array
    *c // will give starting character of array
    

    【讨论】:

      【解决方案2】:

      mystring 已经是一个指针。 *mystring 取消引用它以给出第一个字符。你只想传入mystring

      您还必须为mystring 分配一些内存,无论是静态的还是使用malloc 动态的。

      float slope = 0.0626;
      char* mystring;
      /* char mystring[50]; */ // or just do this, as Sahu and John suggested
      int8_t    LCDline1[20];
      
      void myfunction(){
          mystring = (char*)malloc(mystring_size); // set this to any size
          sprintf(mystring, "%ld", (long)(slope * 10000));
          memcpy(LCDline1, mystring, strlen(mystring)+1);
      }
      

      注意,每当您为字符串分配内存时,请确保分配 比字符串的长度多一个,以存储零分隔符(strlen 和许多其他函数都需要这个)

      【讨论】:

      • 我需要的是一个可以是任意大小的字符串,因为斜率的值可以有 3 到 8 位的精度。我在声明中使用了stackoverflow.com/questions/8732325/how-to-declare-strings-in-c。如何声明一个可以更改其内容的指针?
      • @Bob,您可以使用char mystring[50];让您更轻松
      • 这就是动态分配的意义——你可以使用malloc来分配你想要的任何大小(在机器/操作系统限制内);也不要忘记分配一个额外的字符空间来存储零分隔符
      • 为什么不需要声明 sprintf 正在将值写入指针 mystring 的目标?不是把slope*1000的值写入mystring的存储内存地址吗?
      • @willywonkadailyblah:在第一条评论中,OP 声称 slope 的值可以具有 3 到 8 位的精度;我只是相信他的话。当然,如果你想覆盖可以用 32 位整数类型加上符号表示的十进制数字的最大数量。那么是的,你需要一个 11 或 12 元素的数组来存储它。
      【解决方案3】:

      您错误地使用了指针。 “字符串”被定义为一个字符数组,因此当您编写char *mystring 时,您正在声明一个指向字符数组(或字符串)的指针。

      现在,如果您在代码中使用 *mystring 取消引用 mystring,您将获得该数组的第一个元素,它只是一个字符。正如警告所说,这些函数接受 char* 参数,而不是 char

      所以,只需传入指针,无需解引用:

      void myfunction(){
          sprintf(mystring, "%ld", (long)(slope * 10000));
          memcpy(LCDline1, mystring, strlen(mystring)+1);
      }
      

      【讨论】:

      • 我想更好地理解指针,用你写的 sprintf 函数,不会将斜率*1000 的值写入 mystring 的指针,而不是它的目标吗?
      • @Bob:不,sprintf 假定指针是缓冲区的起始地址,并将生成的字符串写入该缓冲区。
      猜你喜欢
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-04
      • 2020-07-31
      • 2021-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多