Solved

char* array

Posted on 2006-06-25
6
363 Views
Last Modified: 2010-04-24
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
0
Comment
Question by:wonjun
6 Comments
 
LVL 25

Accepted Solution

by:
clockwatcher earned 125 total points
ID: 16980648
iBytesReceived = ClientSocketObject.Recv(DataPacket, 128, 0);

DataPacket should be a pointer to your array of chars.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 16987887
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

0
 

Expert Comment

by:patti_nyl
ID: 16992050
Hi!

Remove the & sign!

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


Patrik
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question