Link to home
Start Free TrialLog in
Avatar of wonjun
wonjun

asked on

char* array

I have this code

char             DataPacket[128];
SocketObject ClientSocketObject;

iBytesReceived = ClientSocketObject.Recv(&DataPacket, 128, 0);

and, Recv is defined as,

int SocketObject::Recv( char *szBuffer, int iBufLen, int iFlags)
{
...
}


I'm using Visual Studio.Net 2005,

and when i compile,
i get this error

error C2664: 'SocketObject::Recv' : cannot convert parameter 1 from 'char (*__w64 )[128]' to 'char *'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


However, this error goes away if i do,

iBytesReceived = ClientSocketObject.Recv(&DataPacket[0], 128, 0);  isntead of
iBytesReceived = ClientSocketObject.Recv(&DataPacket, 128, 0);

and I was wondering if this is the correct way to solve the error. Any explnation will be appreciated. I belive this error does not occur in the old Visual C++ compilers.

Thank you very much
ASKER CERTIFIED SOLUTION
Avatar of clockwatcher
clockwatcher

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DanRollins
clockwatcher is correct...

If you have an array...
     char   DataPacket[128];
then...
    DataPacket[0] is a single char (the first one in the array)
    &DataPacket[0] is a pointer to a char (char*)
         (the same as.... )
    DataPacket is a pointer to char (char*)
         (however...)
    &DataPacket is a pointer to that pointer (it's a char**)

The prototype requires a pointer-to-char, so either of these will work:
    &DataPacket[0]
    DataPacket

Avatar of patti_nyl
patti_nyl

Hi!

Remove the & sign!

will result to this
iBytesReceived = ClientSocketObject.Recv(DataPacket, 128, 0);


Patrik