本文介绍了什么类型是“char x [2]中的x;”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为:


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

解决方案




因为数组不是指针。因为数组不能分配给
。因为运算符''new''

的返回类型是一个指针,所以即使你可以分配给一个数组,

它是错误的类型(它不是指针。


-Mike





不,''x''这里的类型是''char [2]''。其余的如下。


-

祝你好运,

Andrey Tarasevich





的确如此。我应该。我有点尴尬,但我的困惑

来自一些半合法来源。首先,您可以使用数组进行指针算法。例如,以下打印

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

解决方案



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




No, ''x'' here is of type ''char[2]''. The rest follows.

--
Best regards,
Andrey Tarasevich




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;”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 04:36