non-pointer type `char' from NULL的问题

eugenekim 2006-04-20 08:50:22
#include <iostream>
using namespace std;

char *strncat(char *destination, const char *source, int n);
int main()
{
char *str1=new char[256];
char *str2=new char[256];
cout << "Input string1 and Press Enter: " << endl;
cin.getline(str1,256,'\n');
cout << "Input string2 and Press Enter: " << endl;
cin.getline(str2,256,'\n');
strncat(str1,str2,2);
cout << "The result is:" << endl << str1;
return 0;
}

char *strncat(char *destination, const char *source, int n)
{
char *original=destination;
int i=0;
while (*destination)
destination++;
while ((i++<n) && (*destination++=*source++))
;
if (i>n)
*destination=NULL;
return original;
}

g++ strncat.cpp -g -o strncat
出现警告:
strncat.cpp: In function `char* strncat(char*, const char*, int)':
strncat.cpp:27: warning: assignment to non-pointer type `char' from NULL
strncat.cpp:27: warning: argument to non-pointer type `char' from NULL

第27行是:*destination=NULL;
这个警告是什么意思啊?
...全文
479 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
eugenekim 2006-04-21
  • 打赏
  • 举报
回复
改为*destination='\0';就可以了
healer_kx 2006-04-21
  • 打赏
  • 举报
回复
*destination='\0'
eugenekim 2006-04-20
  • 打赏
  • 举报
回复
我后来将27行的*destination=NULL; 改为destination=NULL; 编译的时候没有出错,但是结果出错了。
比如我输入第一个字符串是123456,第二个字符串是123,正确的结果是取第二个字符串前2位加到第一个那里成为12345612。令人奇怪的是这时候结果是12345612iles=C:\Program Files\Common Files

为什么啊?
sharpdew 2006-04-20
  • 打赏
  • 举报
回复
ft
jixingzhong 2006-04-20
  • 打赏
  • 举报
回复
*destination 不是指针了,是一个 char
destination 才是 char * ,才是指针...
jixingzhong 2006-04-20
  • 打赏
  • 举报
回复
*destination=NULL
==》
destination=NULL

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧