为什么输出是abcd???

mirroatl53 2013-08-27 11:32:50

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
TCHAR buf[8] = L"abcdef";
char buf2[8*2];
memcpy(buf2,buf,8);
printf("%ws\n",buf2);
return 0;
}

...全文
251 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
nextseconds 2013-09-04
  • 打赏
  • 举报
回复
引用 5 楼 studyCBC 的回复:
这上面解决也还可以 memcpy(buf2,buf,8);//顺便说下,这个是经常性发生的错误,建议用sizeoof(char)*16防止越界
ztenv 版主 2013-08-27
  • 打赏
  • 举报
回复
引用 2 楼 whizer 的回复:
[quote=引用 1 楼 max_min_ 的回复:]

//估计你这是在UNICODE下的
memcpy(buf2,buf,sizeof(TCHAR)*8);    
++[/quote] ++++
whizer 2013-08-27
  • 打赏
  • 举报
回复
引用 1 楼 max_min_ 的回复:

//估计你这是在UNICODE下的
memcpy(buf2,buf,sizeof(TCHAR)*8);    
++
max_min_ 2013-08-27
  • 打赏
  • 举报
回复

//估计你这是在UNICODE下的
memcpy(buf2,buf,sizeof(TCHAR)*8);    
兆帅 2013-08-27
  • 打赏
  • 举报
回复
[quote=引用 10 楼 xiaohuh421 的回复:] memcpy(buf2,buf,8); 因为这里你只复制了8个字节, 而你的buf中保存的数据应该是14个字节(加了结束符). 还有char buf2[8*2];未初始化, 很容易造成乱码现象. 最好是这么写 char buf2[8*2] = {0}; 定义即初始化 RAII思想.[/quote++
xiaohuh421 2013-08-27
  • 打赏
  • 举报
回复
memcpy(buf2,buf,8); 因为这里你只复制了8个字节, 而你的buf中保存的数据应该是14个字节(加了结束符). 还有char buf2[8*2];未初始化, 很容易造成乱码现象. 最好是这么写 char buf2[8*2] = {0}; 定义即初始化 RAII思想.
Nlessh 2013-08-27
  • 打赏
  • 举报
回复
#ifdef _UNICODE
typedef wchar_t TCHAR
#else
typedef char THCAR
#endif

buf数组中每个字符占用两个字节,加上末尾'\0',总共14个字节
赵4老师 2013-08-27
  • 打赏
  • 举报
回复
不要自己发明format,除非自己能修改printf的实现。
Format Specification Fields: printf and wprintf Functions
A format specification, which consists of optional and required fields, has the following form:

%[flags] [width] [.precision] [{h | l | I64 | L}]type

Each field of the format specification is a single character or a number signifying a particular format option. The simplest format specification contains only the percent sign and a type character (for example, %s). If a percent sign is followed by a character that has no meaning as a format field, the character is copied to stdout. For example, to print a percent-sign character, use %%.

The optional fields, which appear before the type character, control other aspects of the formatting, as follows:

type

Required character that determines whether the associated argument is interpreted as a character, a string, or a number (see Table R.3).

flags

Optional character or characters that control justification of output and printing of signs, blanks, decimal points, and octal and hexadecimal prefixes (see Table R.4). More than one flag can appear in a format specification.

width

Optional number that specifies the minimum number of characters output. (See printf Width Specification.)

precision

Optional number that specifies the maximum number of characters printed for all or part of the output field, or the minimum number of digits printed for integer values (see Table R.5).

h | l | I64 | L

Optional prefixes to type-that specify the size of argument (see Table R.6).
赵4老师 2013-08-27
  • 打赏
  • 举报
回复
对学习编程者的忠告: 眼过千遍不如手过一遍! 书看千行不如手敲一行! 手敲千行不如单步一行! 单步源代码千行不如单步对应汇编一行! VC调试时按Alt+8、Alt+7、Alt+6和Alt+5,打开汇编窗口、堆栈窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应堆栈、内存和寄存器变化,这样过一遍不就啥都明白了吗。 对VC来说,所谓‘调试时’就是编译连接通过以后,按F10或F11键单步执行一步以后的时候,或者在某行按F9设了断点后按F5执行停在该断点处的时候。 (Turbo C或Borland C用Turbo Debugger调试,Linux或Unix下用GDB调试时,看每句C对应的汇编并单步执行观察相应内存和寄存器变化。)
Universe_Admin 2013-08-27
  • 打赏
  • 举报
回复
在项目-属性-常规-字符集中,改成使用多字节字符集 TCHAR buf[8] = "abcdef"; //L去掉 printf("%s\n",buf2); //w去掉 再编译就正常了
七擒关羽 2013-08-27
  • 打赏
  • 举报
回复
这上面解决也还可以 memcpy(buf2,buf,8);//顺便说下,这个是经常性发生的错误,建议用sizeoof(char)*16防止越界
一叶之舟 2013-08-27
  • 打赏
  • 举报
回复
tchar,8个字节实际上是4个字符。

64,664

社区成员

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

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