c++ - C++ 将指针传递给期望 void* 的函数以两种不同的方式工作,为什么?
问题描述
最近我偶然发现了我融入这个例子的代码:
void PrintThis (void* input)
{
std::cout << input << std::endl;
std::cout << (char*)input << std::endl;
}
int main(void)
{
char question[] = "Why does it work?";
PrintThis(question); // output: address + 'Why does it work?' - expected!
PrintThis(&question); // output: address + 'Why does it work?' - unexpected
char* thisIsDiff = "This is different";
PrintThis(thisIsDiff); // output: address + 'This is different' - expected
PrintThis(&thisIsDiff); // output: address + rubbish - expected
return 0;
}
请注意标有意外的行。为什么输出与上面的行相同?为什么当函数参数是 char* 而不是 void 时它甚至无法编译?
解决方案
推荐阅读
- c# - ASP.NET Core 为 IdentityUser 添加二级密码
- wpf - 如何从 Powershell 运行空间连接回主 Powershell 线程?
- google-chrome-extension - Chrome 扩展将变量传递给在“document.start”上运行的脚本
- node-red - Sentry.io 如何集成到 Node-RED 中?
- c# - c# android中是否存在记录如何检查Sqlite
- java - Spring Boot - 在 docker 容器内构建 maven 项目(JAR 文件)
- javascript - 仅在特定页面上加载脚本
- ios - Xcode Swift 如何从字符串在 MFMailComposeViewController 中添加多个 PDF 文档作为附件
- r - 删除字符串中的重复字符
- windows-installer - “msiexec”如何获取平台信息