How to convert const char * to const char

I want to send a user defined text string from a client to server application.



Here is the code that sends the data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#define DEFAULT_BUFLEN 512

int recvbuflen = DEFAULT_BUFLEN;

char *sendbuf;

char recvbuf[DEFAULT_BUFLEN];

int iResult;

// Send an initial buffer
iResult = send(ConnectSocket, sendbuf, (int) strlen(sendbuf), 0);
if (iResult == SOCKET_ERROR) {
    printf("send failed: %d\n", WSAGetLastError());
	system("pause");
    closesocket(ConnectSocket);
    WSACleanup();
    return 1;
}



As you can see, the variable type for sendbuf (the actual text string) needs to be const char *. I want that variable to equal char userinput.

Example:

*sendbuf = userinput;


This is where the error comes in. "Cannot convert const char * to const char"

Any help would be appreciated. Thanks.
I am unclear about what you're trying to do.

Are you trying to send a single char via your socket?

And how is userinput declared?
I want *sendbuf to equal userinput.

userinput is declared somewhere else in the program as "char userinput;"

The following function requires the sendbuf variable to be "const char *":

"send(ConnectSocket, sendbuf, (int) strlen(sendbuf), 0);"


I want to use "cin >> userinput;" and ask the user to input a text string. I then want the contents of userinput to put into char *sendbuf.
If userinput is just a single char, as char userinput; suggests, then you can send it like this

1
2
3
4
5
6
7
8
9
10
// usual include files assumed...

char userinput;

cout << "input a char: ";
cin >> userinput;

char *sendbuf = &userinput; // take the address of char

send(ConnectSocket, sendbuf, 1, 0); // send single char 


or you can skip sendbuf variable and pass address of char to send()

1
2
3
4
5
6
7
8
// usual include files assumed...

char userinput;

cout << "input a char: ";
cin >> userinput;

send(ConnectSocket, &userinput, 1, 0); // send single char 


Is this what you want to do??

Andy
Last edited on
I think your probably want something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#define DEFAULT_BUFLEN 512

int recvbuflen = DEFAULT_BUFLEN;

char sendbuf[DEFAULT_BUFLEN];

std::cin.getline(sendbuf, DEFAULT_BUFLEN);

char recvbuf[DEFAULT_BUFLEN];

int iResult;

// Send an initial buffer
iResult = send(ConnectSocket, sendbuf, (int) strlen(sendbuf), 0);
if (iResult == SOCKET_ERROR) {
    printf("send failed: %d\n", WSAGetLastError());
	system("pause");
    closesocket(ConnectSocket);
    WSACleanup();
    return 1;
}

But if I were you I would use std::string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#define DEFAULT_BUFLEN 512

int recvbuflen = DEFAULT_BUFLEN;

std::string request;

std::getline(std::cin, request);

char recvbuf[DEFAULT_BUFLEN];

int iResult;

// Send an initial buffer
iResult = send(ConnectSocket, request.c_str(), request.length(), 0);
if (iResult == SOCKET_ERROR) {
    printf("send failed: %d\n", WSAGetLastError());
	system("pause");
    closesocket(ConnectSocket);
    WSACleanup();
    return 1;
}
Topic archived. No new replies allowed.