【问题标题】:pass a value from char array to BoS function in c [closed]将char数组中的值传递给c中的BoS函数[关闭]
【发布时间】: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 期望第三个参数是字符串,而不是单个字符。

标签: c arrays for-loop


【解决方案1】:

您需要取消引用您的指针。大概在您正在调用的函数之一中,可能在 text2() 或 text() 中。在不了解更多功能规格的情况下无法告诉您更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    • 2013-11-20
    • 2018-12-02
    • 2012-07-26
    • 2023-03-25
    相关资源
    最近更新 更多