这意味着什么?
What is means of this?
如果函数在c++中是这样定义的:
char *Func() {
return "Text";
}
,
Func()[] = 'a';
意味着什么?
这可能意味着面试官想测试你面对无效源代码的反应。
我不知道Func()[] = 'a';
的意图是什么。c++编译器clang 3.4输出如下:
a.cc:3:9: warning: conversion from string literal to 'char *' is deprecated
[-Wc++11-compat-deprecated-writable-strings]
return "Text";
^
a.cc:6:1: error: C++ requires a type specifier for all declarations
Func()[] = 'a';
^~~~
a.cc:6:5: error: function cannot return array type 'int []'
Func()[] = 'a';
一旦你得到它编译(只是)像这样:
#include <iostream>
char *Func() {
return "Text";
}
int main() {
std::cout << Func() << std::endl;
Func()[0] = 'P';
std::cout << Func() << std::endl;
return 0;
}
你得到这个:
Compiling the source code....
$g++ -std=c++11 main.cpp -o demo -lm -pthread -lgmpxx -lgmp -lreadline 2>&1
main.cpp: In function ‘char* Func()’:
main.cpp:5:9: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
return "Text";
^
Executing the program....
$demo
Text
Segmentation fault (core dumped)
相关文章:
- 这行代码在C++类中意味着什么
- 这对"With a stackless coroutine, only the top-level routine may be suspended."意味着什么
- @CPPFLAGS@在 Makefile.in 中意味着什么?
- 生成文件"relink"意味着什么?
- 从二进制流中读取时,将双精度变量的地址转换为 char* 意味着什么?
- 在这种情况下,"typename..."意味着什么?
- "in-situ without memory allocation" 字符串的愚蠢实现意味着什么?
- 使用typedef有什么用,它意味着什么
- GCC 的 -Wpsabi 选项究竟有什么作用?压制它意味着什么?
- 这在C++ "It does not own the underlying data, and so is cheap to copy or assign"中意味着什么
- 由于某种原因,我的代码中出现了 [json.exception.type_error.302]。我知道错误意味着什么,但我不知道哪里有故障
- 调用堆栈显示 SIGBUS,这意味着什么
- C++标准在[basic.scope.hiding]中"same scope"到底意味着什么?
- 评估对象的创建意味着什么?
- 当我们在 C++ 中说"initialize the object"时,它实际上意味着什么?
- C++这种结构"InterceptionKeyStroke &kstroke = * (InterceptionKeyStroke *) &stroke"意味着什么?
- 该语法在C 中意味着什么
- 链接标志对静态库意味着什么
- 由mpglib输出的"hip: Can't rewind stream by 74 bits"到底意味着什么?
- 这在 C# 中意味着什么以及如何使用它