【发布时间】:2015-06-25 18:00:09
【问题描述】:
我对 C 有点生疏,我想将几个字符串和浮点数连接在一起。特别是,我想制作字符串“AbC”,其中 A 和 C 是字符串文字,b 是浮点数。我知道我必须将浮点数转换为字符串,但我的代码没有编译。下面是我的代码,后面是 gcc 的输出。有关如何修复我的代码的任何建议?
我的程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
double b = 0.5;
char mystring[16];
strcpy(mystring,"A");
strcat(mystring,ftoa(b));
strcat(mystring,"C");
printf("%s",mystring);
return 0;
}
GCC 输出:
test2.c: In function ‘main’:
test2.c:11:1: warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast [enabled by default]
strcat(mystring,ftoa(b));
^
In file included from test2.c:3:0:
/usr/include/string.h:137:14: note: expected ‘const char * __restrict__’ but argument is of type ‘int’
extern char *strcat (char *__restrict __dest, const char *__restrict __src)
^
/tmp/cc77EVEN.o: In function `main':
test2.c:(.text+0x42): undefined reference to `ftoa'
collect2: error: ld returned 1 exit status
【问题讨论】: