Hello, I know this is probably a very elementary answer, but how does one convert a character into a character pointer, for use strcpy()? For instance, I need this:

char sendBuffer[5];
char ch;
// assume code in between makes ch something
strcpy(sendBuffer, (char*) ch);

However, the console window that runs this closes as soon as it gets to this. ch is just a regular character, though.

Sorry that I either do not know or just forgot this, I have tried a lot of things to make it work but have not had any succeed yet.

Recommended Answers

All 6 Replies

Gah, sorry, just realized a solution;

string temp = ch;
strcpy(sendBuffer, temp.c_str());

Sorry for posting :(

you don't need strcpy() at all

char sendBuffer[5];
char ch;
// assume code in between makes ch something
sendBuff[0] = ch;
sendBuff[1] = 0;

> strcpy(sendBuffer, temp.c_str());
If you're going to do this, why are you bothering with 'C' strings at all?

Salem, I need to send the buffer through a socket, and that is what I had already used. I suppose it may not be neccessay.
Ancient Dragon, I had tried simply accessing it that way, but for some reason it wasn't recv()'d correctly.

you can use std::string object in socket programming. just use its c_str() method to convert from std::string to char*.

>> I had tried simply accessing it that way, but for some reason it wasn't recv()'d correctly.

Then there is something else wrong with your program. you should look elsewhere for the error. just make sure the last byte of the buffer is a 0, to terminate the string.

Yes, I thought I could. I also thought that since that was so obvious, there might have been some exception.... I guess not. And I'd prefer just using c_str() than mutating a string, although I don't know liminally why.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.