C++新手,关于strcat连接变量字符串问题

C++语言 码拜 9年前 (2015-05-11) 1055次浏览 0个评论
 
// teststrcat.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
	char strBuffert[256] ={0};
    DWORD dwSize2 = 256;
    GetUserName(strBuffert,&dwSize2);
    char winexe11[256] = "C:\Users\";
	char winexe22[256] = "\AppData\Roaming\tian\tian.exe";
	char winexe77[256] = "\AppData\Local\Temp\zzzz.exe";
	char* winexe44 = strcat(winexe11,strBuffert);
	char* winexe55 = strcat(winexe44,winexe22);
	char* winexe88 = strcat(winexe11,strBuffert);
	char* winexe99 = strcat(winexe88,winexe77);
	cout<<winexe44<<"==="<<winexe99<<endl;
	return 0;
}

为什么运行结果winexe44得到的是C:\Users\Jevon\AppData\Roaming\tian\tian.exeJevon\AppData\Local\Temp\zzzz.exe,而不是C:\Users\Jevon\AppData\Roaming\tian\tian.exe.
winexe99得到的是也是同一个值,而不是C:\Users\Jevon\AppData\Local\Temp\zzzz.exe
C++新手,关于strcat连接变量字符串问题

char *strcat(char *dest, const char *src);

RETURN VALUE
       The strcat() function return a pointer to the resulting string dest.

winexe44 winexe99 都指向 winexe11    必然结果相同
20分
winexe44 winexe99 都指向 winexe11    必然结果相同
20分
楼上正解!winexe44指向的是winexe11的地址,没执行一句都在后面加了内容
你逐行输出一下:

        char* winexe44 = strcat(winexe11,strBuffert);
	cout << winexe44 << endl;
	char* winexe55 = strcat(winexe44,winexe22);
	cout << winexe44 << endl;
	char* winexe88 = strcat(winexe11,strBuffert);
	cout << winexe44 << endl;
谢谢各位了,之前学的是C#,感觉两者在连接字符串区别很大,现在还是理解不了,自己再慢慢学,不懂的再来问各位,谢谢了!

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C++新手,关于strcat连接变量字符串问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!