Is there something wrong with my code below? I got a compilation error!
typedef unsigned char BYTE; void foo(char* & p) { return; } int main() { BYTE * buffer; // error C2664: 'foo' : cannot convert parameter 1 from 'char *' to 'char *&' foo ((char*)buffer); return 0; }
Thanks in advance George
Clicking BYTE*on it char*creates an unnamed temporary object with a type char*. The function being called takes a reference to char*, but you cannot reference such a temporary entity because it is not a real variable.
BYTE*
char*
You can perform reinterpret_cast<char*&>instead of static casting
reinterpret_cast<char*&>
foo (reinterpret_cast<char*&>(buffer));
Or you can make the argument a const reference:
void foo(char* const & p) { return; }
foo . buffer - BYTE. , .
:
1) , , "&"; p. , .
2) , :
BYTE * buffer; char * b = (char *) buffer; foo (b); buffer = (BYTE*) b; // because foo may change b
buffer - "lvalue", , :
buffer
(char*) buffer
"rvalue" ( " rvalue" - , ++ 0x). r.
const r. , :
void foo(char* const& p) // added 'const'
. , lvalues, rvalues :
" rvalue", ++ 0x, , lvalues rvalues, ++ 98. , .
, char *, .
, :
BYTE * buffer; char* ptr = (char*)buffer; foo(ptr); // now you have a matching variable to refer to.
, .
-,
(char)buffer
- - , char.
-, , , .
So yes, there are at least two things in your code.
Write this:
int main() { BYTE * buffer; char* pbuf = (char*)buffer; foo(pbuf); }
Source: https://habr.com/ru/post/1703242/More articles:Writing a GUI for the BRCAPRO - rКомпилятор не создает templated ostream << оператор - c++Should the LINQ context be closed each time? - .netГде рекомендуемое место для хранения экземпляров переменных для макетов - rubyInsert element in LINQ to SQL datacontext - not displayed until application restarts - asp.net-mvcConceptual framework with long-term object context - .netIs there a Drupal module for importing text and images? - drupalModifying app.config file with Visual Studio extension - visual-studio-2008What is the best way to monitor community based websites like stackoverflow, digg etc.? - rssHow to write a GNOME applet with Ruby - ruby | fooobar.comAll Articles