Reputation: 19664
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!
Upvotes: 0
Views: 209
Reputation: 272507
char *buffer[256]
declares an array of 256 pointers-to-char
. You probably want char buffer[256]
.
Upvotes: 1
Reputation: 40246
You wrote:
char * buffer[256];
That is, an array of 256 char pointers.
It seems to me what you really want is:
char buffer[256];
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 where read()
returns zero (likely disconnect) or when it fails and sets errno
to EINTR
(meaning you got a signal while inside read
and you should try again).
Upvotes: 4