I’m working through the curve to learn C and I don’t understand why compiler is reporting this this warning.
I have a char* that is declared in global space (outside of any function). I want to read a file descriptor and return it’s contents to the calling code.
At the top of the file I have the buffer declared:
char * buffer[256];
And this is the function that returns the char*
char * readMessage()
{
int n;
bzero(buffer,256);
n = read(sockfd,buffer,255);
if(n < 0)
error("Error reading from the socket");
return buffer;
}
How can the return type be of an incompatible type? Thanks!
You wrote:
That is, an array of 256 char pointers.
It seems to me what you really want is:
Or, an array of 256 characters (also known as a ‘string’ :-))
Off-topic, but related to your code: This is not a good way to read from a socket. Chances are,
read()will at some point return fewer characters than you expect. You should have some mechanism in place to communicate the length of your messages, and read from the socket in a loop, buffering characters until you get a complete message. You should also handle the case whereread()returns zero (likely disconnect) or when it fails and setserrnotoEINTR(meaning you got a signal while insidereadand you should try again).