Reputation: 13
I have some C code below:
char update[MAX_UPDATE_LEN];
int remoteLen;
char pholder;
pholder = recv(update,connectDescriptor,MAX_UPDATE_LEN,MSG_DONTWAIT); //error
remoteLen = atoi("pholder");
I keep getting the following errors:
client.cpp:849: error: invalid conversion from `char*' to `int'
client.cpp:849: error: initializing argument 1 of `ssize_t recv(int, void*,
size_t, int)'
client.cpp:849: error: invalid conversion from `int' to `void*'
client.cpp:849: error: initializing argument 2 of `ssize_t recv(int, void*,
size_t, int)'
What is causing these errors?
Upvotes: 1
Views: 792
Reputation: 14619
There are several issues with this code:
char update[MAX_UPDATE_LEN];
int remoteLen;
char pholder;
pholder = recv(update,connectDescriptor,MAX_UPDATE_LEN,MSG_DONTWAIT); <-- error here
remoteLen = atoi("pholder");
recv
returns an ssize_t
which is usually much bigger than a char
. So you can't safely store the return code in pholder
.recv()
are in the wrong order. Here's the declaration: ssize_t recv(int sockfd, void *buf, size_t len, int flags);
atoi
is being passed a string which is not a number "pholder"
. It expects a string like "12345"
. If ASCII-encoded numerals is what you expect, you can give update
to atoi
.Bonus: use sizeof(update)
instead of MAX_UPDATE_LEN
for len
-- that way if the type declaration or size of update
changes, you should still get the expected value.
Here's how you might fix it:
char update[MAX_UPDATE_LEN];
const int flags = MSG_DONTWAIT;
const ssize_t ret = recv(connectDescriptor, update , sizeof(update), flags);
if (-1 == ret)
{
perror("recv");
exit(1);
}
const int remoteLen = strtol(update, update + sizeof(update), 0);
Upvotes: 2