【发布时间】:2015-12-30 22:30:44
【问题描述】:
我仍在为我的程序苦苦挣扎,该程序应该在符号板上打印出字符。
编译器说:
char参数与char*类型不兼容
这是我的代码:
char characters[12] = {"ABCDEFGHIJ\0"};
char numbers[12] = {"0123456789\0"};
for (int vertical = 2; vertical<12; vertical++) {
for (int horizontal = 1; horizontal<2; horizontal++) {
text2(horizontal, vertical, numbers[horizontal]);
color2(horizontal, vertical, BLUE);
}
}
for (int abc = 170; abc < 180; abc++) {
color(abc, BLUE);
text(abc, characters[abc]);
}
实际上应该每次只打印一个字符。 这是两个功能
void color2(int i, int j, int f) {
sprintf_s(nachricht, ">># %d %d 0x%x\n", i, j, f);
sendMessage( nachricht );
void text2(int i, int j, char* f) {
sprintf_s(nachricht, ">>#T %d %d %s\n", i, j, f);
sendMessage( nachricht );
}
color2 为字段着色,text2 在字段上显示字符 这是函数 sendMessage 的代码,它包含在两个提到的函数中
int sendMessage(char *sendbuf ) {
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
int iResult;
int recvbuflen = DEFAULT_BUFLEN;
char recvbuf[DEFAULT_BUFLEN];
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo("localhost", DEFAULT_PORT, &hints, &result);
if ( iResult != 0 ) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
return 1;
}
// Attempt to connect to an address until one succeeds
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
// Connect to server.
iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
if( verbose ) printf("socket connected\n");
break;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
terminate("Unable to connect to server!\n");
}
// Send an initial buffer
iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
if (iResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
if( verbose ) printf("Bytes Sent: %ld\n", iResult);
// shutdown the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
answer[0] = '\0';
// Receive until the peer closes the connection
do {
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if ( iResult > 0 ) {
recvbuf[iResult] = '\0';
if( verbose ) printf("Bytes received: %d %s\n", iResult, recvbuf);
strcat_s( answer, recvbuf );
//printf(">>>%s<<<\n", answer );
} else if ( iResult == 0 ) {
if( verbose ) printf("Connection closed\n");
} else
printf("recv failed with error: %d\n", WSAGetLastError());
} while( iResult > 0 );
// cleanup
closesocket(ConnectSocket);
WSACleanup();
return 0;
}
【问题讨论】:
-
可以发一下text2的原型吗?由于
text(abc, characters[abc]);,您还会遇到运行时错误。 -
是的,顺便说一下是头文件,代码是:void color2(int i, int j, int f);和 void text2(int i, int j, char* f);
-
numbers[horizontal]是 char,但您试图将其作为 char* 传递。将 text2 的最后一个参数更改为 char f。 -
您不需要将
\0放在字符串文字的末尾,它会自动完成。 -
我怀疑
text2期望第三个参数是字符串,而不是单个字符。