0

I'm trying to use strerror_r to get the error from the socket function ,but the code is not compiling .i'm getting the error as invlaid conversion from char * to an int. i have included errorno.hand string.h in my code and i'm using eclipse ide running on ubuntu 12.04 .The code is as below.

int err;
char buffer[50];
result=connect(socketHandle, (struct sockaddr *)&remoteSocketInfo, sizeof(sockaddr_in));

   if(result==0)
   {
      printf("\n connect success ");

   }
   else if(result==-1)
   {
       err = strerror_r(errno,buffer,50);// error in place 
       printf("%d",err);
   }
2
  • strerror_r is returning a char * not int. Commented Oct 11, 2013 at 15:17
  • "i have included errorno.hand string.h in my code" -- Then please show us code with those #include directives. It's always better to show (copy-and-pasted) code than to describe what your code looks like. (If you really included errorno.h, you would have gotten an error message; it's errno.h.) And please copy-and-paste the error message rather than re-typing it. Seemingly subtle nuances can be important. sscce.org Commented Oct 11, 2013 at 16:06

3 Answers 3

2

This is because by default the GNU version of strerror_r() is used, which has the following signature:

char *strerror_r(int errnum, char *buf, size_t buflen);

You can try to undefine the _GNU_SOURCE macro in order to get the standard version of this routine. Something like:

#undef _GNU_SOURCE
#include <string.h>

It might be a good idea to isolate that into its own header file (like xsi_strerror_r.h) so that you can include it only where needed. Or even a wrapper function around strerror_r() so that undefining _GNU_SOURCE will have no other unintended effects. For example:

#ifndef XSI_STRERROR_R
#define XSI_STRERROR_R

#undef _GNU_SOURCE
#include <string.h>

int xsi_strerror_r(int errnum, char *buf, size_t buflen);

#endif

The above would be a header file (xsi_strerror_r.h). You then provide a simple implementation for xsi_strerror_r() in its own source file:

#include "xsi_strerror_r.h"
int xsi_strerror_r(int errnum, char *buf, size_t buflen)
{
    return strerror_r(errnum, buf, buflen);
}

And then you only use xsi_strerror_r() in the rest of your code.

Sign up to request clarification or add additional context in comments.

3 Comments

The #ifdef is unnecessary. #undef does nothing if the macro isn't already defined.
@KeithThompson Indeed.
@KeithThompson : yes keith but i came across is this function in linux so wanted to try .i have been doing win32 programming which uses wgetlasterror to get socket function errors ,so wanted to check if there is an similar function out in linux .
2

Adding

Include <errno.h>

Should solve the problem


Note that the other reason for this error According to docs might be that,

it returns int on XSI-compliant system and returns char* on GNU specific system.

Comments

1

According to http://linux.die.net/man/3/strerror_r, strerror_r can either return a char * or int. The char * version says it's GNU-specific, so if you're using GNU, maybe that's the problem.

3 Comments

i saw a place where the function prototype returned an int . Now how to get the error number ?
@SanthoshPai, I'm not sure what you're asking - the error number is passed as an argument to strerror_r.
@SanthoshPai Why not just printf("Error: %d\n",errno); then?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.