strange char* compile error?
-
Hello everyone, Anything wrong with my code below? I got the compile 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
-
Hello everyone, Anything wrong with my code below? I got the compile 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
this isnt strange as foo required char*& and not char*.
Don't try it, just do it! ;-)
-
this isnt strange as foo required char*& and not char*.
Don't try it, just do it! ;-)
My confusion is reference type is the same as original type, for example when type Foo is needed, we could pass Foo or pass Foo&. So, I think then char* & is needed I could pass char*? What is wrong with my understanding? :-)
-
My confusion is reference type is the same as original type, for example when type Foo is needed, we could pass Foo or pass Foo&. So, I think then char* & is needed I could pass char*? What is wrong with my understanding? :-)
BYTE is an unsigned char (8 bit) char* is a pointer (32 bit) When converting BYTE to char* then it needs to create a temporary variable. It is not possible to make return-by-reference with temporary variables. Instead make the conversion from BYTE to char* by using a local variable, and then pass that local variable to the function.
-
Hello everyone, Anything wrong with my code below? I got the compile 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
You must call it as below
char* pp = (char*)buffer;
foo(pp);«_Superman_»
-
BYTE is an unsigned char (8 bit) char* is a pointer (32 bit) When converting BYTE to char* then it needs to create a temporary variable. It is not possible to make return-by-reference with temporary variables. Instead make the conversion from BYTE to char* by using a local variable, and then pass that local variable to the function.
I agree with your points, but confused. Since I am using BYTE* not BYTE. Anyway, could you show me your code please? I think code clarifies everything. :-) regards, George
-
You must call it as below
char* pp = (char*)buffer;
foo(pp);«_Superman_»
Your code fixes my issue, but why? I think both your code and my code are of the same effect. :-) regards, George
-
Your code fixes my issue, but why? I think both your code and my code are of the same effect. :-) regards, George
When you typecast, you have to give the destination exactly as it should be. So you could also typecast if as
foo((char*& )buffer)
«_Superman_»
-
BYTE is an unsigned char (8 bit) char* is a pointer (32 bit) When converting BYTE to char* then it needs to create a temporary variable. It is not possible to make return-by-reference with temporary variables. Instead make the conversion from BYTE to char* by using a local variable, and then pass that local variable to the function.
Where is he converting a BYTE to char*? He's converting a BYTE* to char*. In other words, unsigned char* to char*. I think there's no problem with the pointer size. this is enough: foo ((char*&)buffer);
-
Where is he converting a BYTE to char*? He's converting a BYTE* to char*. In other words, unsigned char* to char*. I think there's no problem with the pointer size. this is enough: foo ((char*&)buffer);
It's all to do with the semantics of casting - you can't pass a reference to something you've produced with a cast, even if the size and location of the thing is the same.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Hello everyone, Anything wrong with my code below? I got the compile 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
It's all to do with the semantics of casting - you can't pass a non-const reference to something you've produced with a cast, even if the size and location of the thing is the same.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p