什么类型是“char x [2]中的x;”? [英] What type is x in "char x[2];"?
问题描述
我认为:
char x [2];
将x变为指向char的指针。
但是为什么以下代码不会编译:
int main(){
char pbuf [2 ];
pbuf [0] =''a'';
pbuf [1] =''\ 0'';
std :: cout<< pbuf<< std :: endl;
pbuf = new char [2]; // Lvalue必需错误
pbuf [0] =''b'';
pbuf [1] =''\ 0'';
std :: cout<< pbuf<< std :: endl;
delete [] pbuf;
返回0;
}
相比之下,以下代码按预期编译和运行:
int main(){
char * pbuf;
pbuf = new char [2];
pbuf [0] =''a'';
pbuf [1] =''\''' ;
std :: cout<< pbuf<< std :: endl;
delete [] pbuf;
pbuf = new char [2];
pbuf [0] ='''b'';
pbuf [1] =''\ 0'';
std :: cout<< pbuf<< std :: endl;
delete [] pbuf;
返回0;
}
为什么第一个例子不好用?
谢谢,
cpp
" cppaddict" <他*** @ hello.com>在消息中写道
news:k3 ******************************** @ 4ax.com ...我认为:
char x [2];
使x成为指向char的指针。
编号'''''是一个数组。
数组不是指针。
指针不是数组。
但是如何编译以下代码:
#include< iostream> / *表示std :: cout * /
#include< ostream> / * for std :: endl * /
int main(){
char pbuf [2];
pbuf [0] =''a'';
pbuf [1] =''\ 0'';
std :: cout<< pbuf<< std :: endl;
pbuf = new char [2]; // Lvalue必需ERROR
你不能这样做。数组不能像这样分配。
只能分配每个元素。
同样,数组不是指针。指针不是一个数组。
我以为你已经在这里发帖和阅读足够长的
了解到现在。 :-)
pbuf [0] =''b'';
pbuf [1] =''\ 0'';
std :: cout<< ; pbuf<< std :: endl;
delete [] pbuf;
返回0;
}
相反,以下代码按预期编译和运行:
是的,因为''pbuf''是一个指针,你可以指定
a值。
#include< iostream> / *表示std :: cout * /
#include< ostream> / * for std :: endl * /
int main(){
char * pbuf;
pbuf = new char [2];
pbuf [0] ='''';;
pbuf [1] =''\ 0'';
std :: cout<< pbuf<< std :: endl;
delete [] pbuf;
pbuf = new char [2];
pbuf [0] =''b'';
pbuf [1] =''\ 0'';
std :: cout<< pbuf<< std :: endl;
删除[] pbuf;
返回0;
}
为什么第一个例子不工作?
因为数组不是指针。因为数组不能分配给
。因为运算符''new''
的返回类型是一个指针,所以即使你可以分配给一个数组,
它是错误的类型(它不是指针。
-Mike
cppaddict写道:我以为那个:
char x [2];
将x变成指向char的指针。
...
不,''x''这里的类型是''char [2]''。其余的如下。
-
祝你好运,
Andrey Tarasevich
#include< iostream> / *表示std :: cout * /
#include< ostream> / *对于std :: endl * /
已经有了这些。只是不在帖子里......
同样,数组不是指针。一个指针不是一个数组。
我以为你已经在这里发帖和阅读足够长的时间了解它。 : - )
的确如此。我应该。我有点尴尬,但我的困惑
来自一些半合法来源。首先,您可以使用数组进行指针算法。例如,以下打印
b:
int main(){
char pbuf [2];
pbuf [0] =''a'';
pbuf [1] =''b'';
pbuf [2] =''\\ \\ 0'';
std :: cout<< *(pbuf + 1);
返回0;
}
这使它看起来像一个指针。此外,我一直在使用
Windows API,它经常使指针和数组似乎可以互换。例如,请考虑此代码从Rich Edit控件中检索
行:
char pbuf [100];
pbuf [99] =''\''';
pbuf [0] = 99 //指定要检索的最大字符数;
SendMessage(hwndRichEdit,EM_GETLINE ,0,(LPARAM)pbuf);
std :: cout<< 第1行: << pbuf<< std :: endl;
哪个有效。 LPARAM是类型指针我非常肯定。文档
在这里:
http://msdn.microsoft.com/library/de...em_getline.asp
你能解释一下为什么上述两件事情都有效,即使指针和
不同类型的数组?
谢谢,
cpp
I thought that:
char x[2];
made x into a pointer-to-char.
But how come the following code won''t compile:
int main() {
char pbuf[2];
pbuf[0] = ''a'';
pbuf[1] = ''\0'';
std::cout << pbuf << std::endl;
pbuf = new char[2]; //Lvalue Required ERROR
pbuf[0] = ''b'';
pbuf[1] = ''\0'';
std::cout << pbuf << std::endl;
delete[] pbuf;
return 0;
}
In contrast, the following code compiles and runs as expected:
int main() {
char *pbuf;
pbuf = new char[2];
pbuf[0] = ''a'';
pbuf[1] = ''\0'';
std::cout << pbuf << std::endl;
delete[] pbuf;
pbuf = new char[2];
pbuf[0] = ''b'';
pbuf[1] = ''\0'';
std::cout << pbuf << std::endl;
delete[] pbuf;
return 0;
}
Why won''t the first example work?
Thanks,
cpp
"cppaddict" <he***@hello.com> wrote in message
news:k3********************************@4ax.com...I thought that:
char x[2];
made x into a pointer-to-char.
No. ''x'' is an array.
An array is not a pointer.
A pointer is not an array.
But how come the following code won''t compile:
#include <iostream> /* for std::cout */
#include <ostream> /* for std::endl */
int main() {
char pbuf[2];
pbuf[0] = ''a'';
pbuf[1] = ''\0'';
std::cout << pbuf << std::endl;
pbuf = new char[2]; //Lvalue Required ERROR
You Can''t Do That. Arrays cannot be assigned to like that.
Only each individual element can be assigned.
Again, an array is not a pointer. A pointer is not an array.
I thought you''d been posting and reading here long enough
to know that by now. :-)
pbuf[0] = ''b'';
pbuf[1] = ''\0'';
std::cout << pbuf << std::endl;
delete[] pbuf;
return 0;
}
In contrast, the following code compiles and runs as expected:
Yes, because ''pbuf'' is a pointer, to which you can assign
a value.
#include <iostream> /* for std::cout */
#include <ostream> /* for std::endl */
int main() {
char *pbuf;
pbuf = new char[2];
pbuf[0] = ''a'';
pbuf[1] = ''\0'';
std::cout << pbuf << std::endl;
delete[] pbuf;
pbuf = new char[2];
pbuf[0] = ''b'';
pbuf[1] = ''\0'';
std::cout << pbuf << std::endl;
delete[] pbuf;
return 0;
}
Why won''t the first example work?
Because an array is not a pointer. Because arrays cannot
be assigned to. Because the return type of operator ''new''
is a pointer, so even if you could assign to an array,
it''s the wrong type (it''s not a pointer).
-Mike
cppaddict wrote:I thought that:
char x[2];
made x into a pointer-to-char.
...
No, ''x'' here is of type ''char[2]''. The rest follows.
--
Best regards,
Andrey Tarasevich
#include <iostream> /* for std::cout */
#include <ostream> /* for std::endl */
Already had those. Just weren''t in the post...
Again, an array is not a pointer. A pointer is not an array.
I thought you''d been posting and reading here long enough
to know that by now. :-)
Indeed. I should have. I''m a little embarrassed, but my confusion
comes from some semi-legitimate sources. First, the fact that you can
do pointer arithmetic with arrays. For example, the following prints
b:
int main() {
char pbuf[2];
pbuf[0] = ''a'';
pbuf[1] = ''b'';
pbuf[2] = ''\0'';
std::cout << *(pbuf+1);
return 0;
}
This makes it seem like a pointer. Also, I''ve been working with the
Windows API, which often makes pointers and arrays seem
interchangeable. For example, consider this code for retrieving a
line from a Rich Edit control:
char pbuf[100];
pbuf[99] = ''\0'';
pbuf[0] = 99 //specifies max number of characters to retrieve;
SendMessage(hwndRichEdit,EM_GETLINE,0,(LPARAM)pbuf );
std::cout << "Line 1: " << pbuf << std::endl;
which works. The LPARAM is of type pointer I''m pretty sure. The docs
are here:
http://msdn.microsoft.com/library/de...em_getline.asp
Can you explain why the above two things work even though pointers and
arrays of different types?
Thanks,
cpp
这篇关于什么类型是“char x [2]中的x;”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!