如何“重新定义”一个 char * 到另一个 char *?

发布于 2024-10-08 07:44:27 字数 263 浏览 0 评论 0原文

像这样的事情:(这是任务,如何做到这一点,而不是改变主函数的主体)

我认为它太简单了......但是......我不知道该怎么做......

#include <iostream>

#define "a" "b"

int main( int argc,char ** argv) {
   std::cout << "a";
   return 0;
}

/ /输出:b

怎么办呢?

Something like that: (it is task, how to do this, and not to change body of main function)

I thought that it is too simple... but... I don't know how to do it...

#include <iostream>

#define "a" "b"

int main( int argc,char ** argv) {
   std::cout << "a";
   return 0;
}

// output: b

How to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不交电费瞎发啥光 2024-10-15 07:44:27

幸运的是,您无法重新定义字符串文字。当然,真正的问题是:为什么你想这样做?

Fortunately, you can't redefine string literals. Of course, the real question is: why do you want to do it?

把昨日还给我 2024-10-15 07:44:27

更改 C++ 输出而不更改 main() 函数

例如, 来自该主题:

#include <iostream> 
#include <cstdio> 
#include <sstream> 

#define cout     printf("a"); std::ostringstream os; os 

int main() 
{ 
    std::cout << "b"; 
}

一个更简单的主题:

#include <iostream>
#define cout cout << "a"; return 0; std::cout
int main() 
{ 
    std::cout << "b"; 
}

Changing C++ output without changing the main() function

for example from that topic:

#include <iostream> 
#include <cstdio> 
#include <sstream> 

#define cout     printf("a"); std::ostringstream os; os 

int main() 
{ 
    std::cout << "b"; 
}

a simplier one:

#include <iostream>
#define cout cout << "a"; return 0; std::cout
int main() 
{ 
    std::cout << "b"; 
}
世俗缘 2024-10-15 07:44:27

宏名称必须是标识符,因此不能包含字符 "

Macro names must be identifiers, and thus cannot contain the character ".

雄赳赳气昂昂 2024-10-15 07:44:27

你不能。预处理器不会“查看”字符串文字。

You can't. The preprocessor doesn't "look into" string literals.

通知家属抬走 2024-10-15 07:44:27

也许你想这样做:

#include <iostream>

#define a "b"

int main( int argc,char ** argv) {
   std::cout << a;
   return 0;
}

输出:b

Probably you want to do this:

#include <iostream>

#define a "b"

int main( int argc,char ** argv) {
   std::cout << a;
   return 0;
}

Output : b

德意的啸 2024-10-15 07:44:27

#define 是一种糟糕的 c++ 风格。使用const

#include <iostream>  
// const char * string_a = "a"; // Regular: Use const char *
const char * string_a = "b"; // Replacing: Instead of bad    #define "a" "b"  

int main( int argc,char ** argv) {    
    std::cout << string_a;    
    return 0; 
}  

#define is a bad c++ style. Use const.

#include <iostream>  
// const char * string_a = "a"; // Regular: Use const char *
const char * string_a = "b"; // Replacing: Instead of bad    #define "a" "b"  

int main( int argc,char ** argv) {    
    std::cout << string_a;    
    return 0; 
}  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文