三个小问题
问题1。/怎么实现 int linkstr(void * str1,void *str2);
/******************char的实现************
Copyright(C) 2004-2005 vision,math,NJU.
File Name: guessNumber.cpp
Author: vision Version: 1.0 Data: 17-2-2004
Description: 连接两个已给的字符串,返回一指针。
Other: .
History:修改历史
**************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*************************************************
Function: // link_str
Description: // 输入三个参数,连接后两个参数,将结果存到第三个参数里,然后返回
Calls: //
Called By: // main()
Table Accessed: //被修改的表(此项仅对于牵扯到数据库操作的程序)
Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)
Input: // rstr为最后结果的字符串,且作为返回值,fstr为前字符串参数,bstr为后
//字符串参数,联结方式为fstr+bstr
Output: //
Return: // 如果结果非空返回1否则0
Others: // 其它说明
*************************************************/
int link_str(char *rstr, const char *fstr, const char *bstr)
{
strcpy(rstr,fstr);
char *bstrtmp = (char *)bstr;
/*找到fstr结束位置*/
for(;*rstr;rstr++);
/*江后字符串连接到结果字符串上*/
while(*bstrtmp)
{
*rstr++=*bstrtmp++;
}
*rstr='\0';
/*如果结果非空返回1否则0*/
if(*(rstr-1))
{
return 1;
}
else
{
return 0;
}/* endif*/
}
/*主函数*/
void main()
{
#if 0
char *fronts = "hello";
char *behinds = "world!";
#else
char fronts[81];
char behinds[81];
gets(fronts);
gets(behinds);
#endif
char *results = (char*)malloc(strlen(fronts)+strlen(behinds)+1);
int sucess = link_str(results, fronts, behinds);
if(sucess)
{
printf("连接后字符串为:%s \n",results);
}
else
{
puts("error! no string!");
}/*endif*/
}
问题2。
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char *string = "16", *endptr;
long lnumber;
/* strtol converts string to long integer */
lnumber = strtol(string, &endptr,16); //对十进制正确,但对16进制不能正确运行,结果//lnumber为22。不解!
printf("string = %s long = %#x \n", string, lnumber); //不能输出
printf("c = %#x \n",16);
printf("c = %#o \n",16);
return 0;
}
问题三.
关于 printf()的一个机理问题,不知道为什么?
printf("%f",1000/1000);
printf("%f",(double)1000/1000);
结果为:0
1.000000