如何在C中将char []转换为char*?

Rav*_*ven -1 c type-conversion

问题非常简单.如何转换char[]到一个char*用C?我真的很难找到解决方案.所以你的帮助非常感谢:)

这就是我需要这个的原因:

sprintf(name,"%c%c%%HACKED%c%c.virus",a,b,c,d); // print a formatted text to string (char[])
strcat(buffer,currentPath); // currentPath is of data type char[]
strcat(buffer,"\\");
strcat(buffer,name); // Now this is where the problem comes in. Since strcat function needs a const char* for it's second argument.

printf("%s",name);
Run Code Online (Sandbox Code Playgroud)

NPE*_*NPE 5

你不需要做任何事情.数组自动衰减到指针,所以你的代码看起来很好.

在第一次buffer调用之前,您需要确保以有效的NUL终止字符串开头strcat().它也需要是可写的(例如,它不能是指向字符串文字的指针).

您还需要确保不会溢出buffer.我建议你看一下strncat().