PDA

View Full Version : warning: character constant too long & incompatible type


dooker
01-10-2004, 03:22 PM
I have written the following 2 functions to try and send and print a menu title from a server to connected clients on a network. When I try and compile the server I get the following errors:

chat_sv.c:300:10: warning: character constant too long
chat_sv.c: In function `send_menu':
chat_sv.c:300: incompatible types in assignment
chat_sv.c:301:10: warning: character constant too long
chat_sv.c:301: incompatible types in assignment
chat_sv.c:302:10: warning: character constant too long
chat_sv.c:302: incompatible types in assignment


I have declared my messages as type char msg[25] and then assigned my message to the variable by msg = 'message'; Can anyone tell me why on compiling I am being told the message type is incompatible and the constant too long??

Thanks


funtion from within server program:

int send_menu(int sockfd, int max_fd)
{

short msg_type;
short msg1_len;
short msg2_len;
short msg3_len;
char msg1[25];
char msg2[25];
char msg3[25];


msg1 = '--------------------';
msg2 = 'LIST OF ONLINE USERS';
msg3 = '--------------------';


msg_type = htons(3);
write(sockfd, &msg_type, sizeof(short));


msg1_len = htons(strlen(msg1));
write(sockfd, (char *)&msg1_len, sizeof(short));
write(sockfd, msg1, strlen(msg1));

msg2_len = htons(strlen(msg2));
write(sockfd, (char *)&msg2_len, sizeof(short));
write(sockfd, msg2, strlen(msg2));


msg3_len = htons(strlen(msg3));
write(sockfd, (char *)&msg3_len, sizeof(short));
write(sockfd, msg3, strlen(msg3));

} /* end of send_menu function */

receiving function from within client code:

int process_menu(int sockfd)
{
int nbytes;
short msg1_len;
short msg2_len;
short msg3_len;
char buf[250];

bzero(buf, sizeof(buf));
nbytes = read(sockfd, (char *)&msg1_len, sizeof(short));
msg1_len = ntohs(msg1_len);
nbytes = read(sockfd, buf, msg1_len);
printf("\t%s\n", buf);

bzero(buf, sizeof(buf));
nbytes = read(sockfd, (char *)&msg2_len, sizeof(short));
msg2_len = ntohs(msg2_len);
nbytes = read(sockfd, buf, msg2_len);
printf("\t%s\n", buf);

bzero(buf, sizeof(buf));
nbytes = read(sockfd, (char *)&msg3_len, sizeof(short));
msg3_len = ntohs(msg3_len);
nbytes = read(sockfd, buf, msg3_len);
printf("\t%s\n", buf);

} /* end of process_menu function */


[/quote]

RobSeace
01-10-2004, 08:24 PM
Because, you're using single quotes instead of double quotes, for
one thing... Single quotes are used for single character constants;
double quotes are used for string constants...

Also, you can't just do that assignment to buffers, like that... If
you declared them as simple "char*" pointers, then such an
assignment to a string constant would be fine... OR, if you moved
the assignment to the actual declaration of the "char[]" buffers,
then it'd also be fine, because it would be taken as an initialization...
But, after the fact, in order to fill the buffers with the strings, you
have to use something like strcpy(), instead...