C++ 11标准(ISO/IEC 14882:2011)在§ C.1.1:
char* p = "abc"; // valid in C, invalid in C++
Run Code Online (Sandbox Code Playgroud)
对于C++来说,没关系,因为指向String Literal的指针是有害的,因为任何修改它的尝试都会导致崩溃.但为什么它在C中有效?
C++ 11还说:
char* p = (char*)"abc"; // OK: cast added
Run Code Online (Sandbox Code Playgroud)
这意味着如果将强制转换添加到第一个语句,它将变为有效.
为什么转换使第二个语句在C++中有效?它与第一个语句有什么不同?它不是仍然有害吗?如果是这样的话,为什么标准说它没关系?
我是C的新手,我需要一些处理数组的方法的帮助.来自Java编程,我习惯于说int [] method()能够返回一个数组.但是,我发现使用C时,必须在返回数组时使用指针.作为一个新的程序员,我真的根本不理解这一点,即使我已经查看了许多论坛.
基本上,我正在尝试编写一个在C中返回char数组的方法.我将使用数组提供方法(让我们称之为returnArray).它将从前一个数组创建一个新数组并返回指向它的指针.我只是需要一些关于如何开始这个以及如何在从数组发出指针后读取指针的帮助.任何解释这个的帮助表示赞赏.
建议的数组返回函数的代码格式
char *returnArray(char array []){
char returned [10];
//methods to pull values from array, interpret them, and then create new array
return &(returned[0]); //is this correct?
}
Run Code Online (Sandbox Code Playgroud)
函数的调用者
int main(){
int i=0;
char array []={1,0,0,0,0,1,1};
char arrayCount=0;
char* returnedArray = returnArray(&arrayCount); ///is this correct?
for (i=0; i<10;i++)
printf(%d, ",", returnedArray[i]); //is this correctly formatted?
}
Run Code Online (Sandbox Code Playgroud)
我还没有测试过这个,因为我的C编译器目前还没有工作,但我想弄明白这一点
我在c#中有一个char:
char foo = '2';
Run Code Online (Sandbox Code Playgroud)
现在我想把2变成一个int.我发现Convert.ToInt32返回char的实际十进制值而不是数字2.以下将起作用:
int bar = Convert.ToInt32(new string(foo, 1));
Run Code Online (Sandbox Code Playgroud)
int.parse也适用于字符串.
C#中没有本地函数从char到int而不是字符串吗?我知道这是微不足道的,但似乎很奇怪,直接进行转换没有任何原生的东西.
我在cppreference的文档中看到了这个例子std::numeric_limits
#include <limits>
#include <iostream>
int main()
{
std::cout << "type\tlowest()\tmin()\t\tmax()\n\n";
std::cout << "uchar\t"
<< +std::numeric_limits<unsigned char>::lowest() << '\t' << '\t'
<< +std::numeric_limits<unsigned char>::min() << '\t' << '\t'
<< +std::numeric_limits<unsigned char>::max() << '\n';
std::cout << "int\t"
<< std::numeric_limits<int>::lowest() << '\t'
<< std::numeric_limits<int>::min() << '\t'
<< std::numeric_limits<int>::max() << '\n';
std::cout << "float\t"
<< std::numeric_limits<float>::lowest() << '\t'
<< std::numeric_limits<float>::min() << '\t'
<< std::numeric_limits<float>::max() << '\n';
std::cout << "double\t"
<< std::numeric_limits<double>::lowest() << '\t'
<< std::numeric_limits<double>::min() << '\t'
<< std::numeric_limits<double>::max() …Run Code Online (Sandbox Code Playgroud) 我试图找到一种char从键盘输入的方法.
我试过用:
Scanner reader = new Scanner(System.in);
char c = reader.nextChar();
Run Code Online (Sandbox Code Playgroud)
此方法不存在.
我试着把它c作为一个String.然而,它并不总是适用于所有情况,因为我从我的方法调用的另一种方法需要一个char输入.因此,我必须找到一种方法来明确地将char作为输入.
有帮助吗?
sort(mMyClassVector.begin(), mMyClassVector.end(),
[](const MyClass & a, const MyClass & b)
{
return a.mProperty > b.mProperty;
});
Run Code Online (Sandbox Code Playgroud)
我想使用lambda函数来排序自定义类来代替绑定实例方法.但是,上面的代码会产生错误:
错误C2564:'const char*':对内置类型的函数式转换只能接受一个参数
它工作得很好boost::bind(&MyApp::myMethod, this, _1, _2).
以下是代码段,
int a = 1;
char b = (char) a;
System.out.println(b);
Run Code Online (Sandbox Code Playgroud)
但我得到的是空输出.
int a = '1';
char b = (char) a;
System.out.println(b);
Run Code Online (Sandbox Code Playgroud)
我将得到1作为我的输出.
有人可以解释一下吗?如果我想在第一个片段中将int转换为char,我该怎么办?
如何在计算JavaScript项目的数字时使用模运算符(%)?
在C++中sizeof('a') == sizeof(char) == 1.这具有直观意义,因为它'a'是一个字符文字,并且sizeof(char) == 1由标准定义.
然而,在C中sizeof('a') == sizeof(int).也就是说,看起来C字符文字实际上是整数.有谁知道为什么?我可以找到很多关于这个C怪癖的提及,但没有解释为什么它存在.