type cast error

A

Andre

The line of code "socklen_t sourceAddressLength = sizeof(struct
sockaddr_in);" is generating the error "error: invalid conversion from
‘int*’ to ‘socklen_t*’".

Does anyone know how can I fix it? I'm compiling this code on a mac
using gcc 4.

Thanks in advance
 
A

Andre

The line of code "socklen_t sourceAddressLength =  sizeof(struct
sockaddr_in);" is generating the error "error: invalid conversion from
‘int*’ to ‘socklen_t*’".

Does anyone know how can I fix it? I'm compiling this code on a mac
using gcc 4.

Thanks in advance

Actually, I'm getting the following errors

Network.cpp:112: error: invalid conversion from ‘int*’ to ‘socklen_t*’
Network.cpp:112: error: initializing argument 6 of ‘ssize_t recvfrom
(int, void*, size_t, int, sockaddr*, socklen_t*)’

regarding the following line of code, and not "socklen_t
sourceAddressLength = sizeof(struct sockaddr_in);" as I said before.

* messageToReceiveLength = recvfrom(localSocketFd,
messageToReceive,
* messageToReceiveLength,
0,
(struct sockaddr *)
&sourceAddress,
static_cast<socklen_t *>
(&sourceAddressLength));
 
S

SG

[...] Does anyone know how can I fix it? [...]

Network.cpp:112: error: invalid conversion from ‘int*’ to ‘socklen_t*’
Network.cpp:112: error:   initializing argument 6 of ‘ssize_t recvfrom
(int, void*, size_t, int, sockaddr*, socklen_t*)’

regarding the following line of code, and not "socklen_t
sourceAddressLength =  sizeof(struct sockaddr_in);" as I said before.

      * messageToReceiveLength = recvfrom(localSocketFd,
                                          messageToReceive,
                                          * messageToReceiveLength,
                                          0,
                                          (struct sockaddr *)
&sourceAddress,
                                          static_cast<socklen_t *>
(&sourceAddressLength));

Try to avoid "casting" altogether. I doubt that your case there's a
need for casting. Obviously socklen_t is neither an int nor an
unsigned int. Otherwise it would compile. Why don't you simply use a
variable of type socklen_t instead of int for sourceAddressLength?
The use of "(struct sockaddr *)" is especially dangerous in terms of
type safety.

Cheers!
SG
 
D

Daniel Pitts

Andre said:
The line of code "socklen_t sourceAddressLength = sizeof(struct
sockaddr_in);" is generating the error "error: invalid conversion from
‘int*’ to ‘socklen_t*’".

Does anyone know how can I fix it? I'm compiling this code on a mac
using gcc 4.

Thanks in advance

I doubt that is the actual error for that line of code, since there is
no "*" anywhere on that code.

Are you sure it isn't after that line, or that you copied the error
incorrectly?
 
A

Andre

I doubt that is the actual error for that line of code, since there is
no "*" anywhere on that code.

Are you sure it isn't after that line, or that you copied the error
incorrectly?

I made a big mess with this post :-(

I'm getting the following errors

Network.cpp:112: error: invalid conversion from ‘int*’ to ‘socklen_t*’
Network.cpp:112: error: initializing argument 6 of ‘ssize_t recvfrom
(int, void*, size_t, int, sockaddr*, socklen_t*)’

regarding the following piece of code

socklen_t sourceAddressLength = sizeof(struct sockaddr_in);
* messageToReceiveLength = recvfrom(localSocketFd,
messageToReceive,
* messageToReceiveLength,
0,
(struct sockaddr *)
&sourceAddress,
&sourceAddressLength);

I also tried "socklen_t sourceAddressLength = static_cast<socklen_t>
(sizeof(struct sockaddr_in));", but I get the same errors.
 
J

James Kanze

[...]
Network.cpp:112: error: invalid conversion from \u2018int*\u2019 to \u2018socklen_t*\u2019
Network.cpp:112: error: initializing argument 6 of \u2018ssize_t recvfrom
(int, void*, size_t, int, sockaddr*, socklen_t*)\u2019

regarding the following piece of code

socklen_t sourceAddressLength = sizeof(struct sockaddr_in);
* messageToReceiveLength = recvfrom(localSocketFd,
messageToReceive,
* messageToReceiveLength,
0,
(struct sockaddr *)
&sourceAddress,
&sourceAddressLength);
I also tried "socklen_t sourceAddressLength =
static_cast<socklen_t> (sizeof(struct sockaddr_in));", but I
get the same errors.

I can't reproduce it on my maching, but a priori, from the
error message, you have a problem with the definition of
socklen_t (which is required by Posix to be an integer type with
at least 32 bits). My guess is that somewhere locally there is
a definition of socklen_t, to something different from what it
is defined as in the Posix header.
 
A

Andre

    [...]


Network.cpp:112: error: invalid conversion from \u2018int*\u2019 to \u2018socklen_t*\u2019
Network.cpp:112: error:   initializing argument 6 of \u2018ssize_t recvfrom
(int, void*, size_t, int, sockaddr*, socklen_t*)\u2019
regarding the following piece of code
      socklen_t sourceAddressLength =  sizeof(struct sockaddr_in);
      * messageToReceiveLength = recvfrom(localSocketFd,
                                          messageToReceive,
                                          * messageToReceiveLength,
                                          0,
                                          (struct sockaddr *)
&sourceAddress,
                                          &sourceAddressLength);
I also tried "socklen_t sourceAddressLength =
static_cast<socklen_t> (sizeof(struct sockaddr_in));", but I
get the same errors.

I can't reproduce it on my maching, but a priori, from the
error message, you have a problem with the definition of
socklen_t (which is required by Posix to be an integer type with
at least 32 bits).  My guess is that somewhere locally there is
a definition of socklen_t, to something different from what it
is defined as in the Posix header.

--
James Kanze (GABI Software)             email:[email protected]
Conseils en informatique orientée objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Hi James,

It turned out socklen_t is not defined in the mac, and I just needed
to define it as u_int32_t.

Thanks,

Andre
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,781
Messages
2,569,619
Members
45,316
Latest member
naturesElixirCBDGummies

Latest Threads

Top