By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,805 Members | 1,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,805 IT Pros & Developers. It's quick & easy.

Casting char to char*

P: n/a
How do I cast or promote a char variable to a char* variable? For
example, I want to use strcat to append a character to an existing
"string". (BTW, I'm not able to use STL string or CString data
types...) TIA
Sep 19 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"Michael R. Copeland" <mr*****@cox.net> wrote in message
news:MP************************@news.west.cox.net. ..
How do I cast or promote a char variable to a char* variable? For
example, I want to use strcat to append a character to an existing
"string". (BTW, I'm not able to use STL string or CString data
types...) TIA


char CharStr[20];
strcpy(CharStr, "Test";
char SomeChar = 'A';

char TempStr[2];
TempStr[0] = SomeChar;
TempStr[1] = '\0';

strcat( CharStr, TempStr );

That's one way.
Sep 19 '05 #2

P: n/a
> How do I cast or promote a char variable to a char* variable? For
example, I want to use strcat to append a character to an existing
"string". (BTW, I'm not able to use STL string or CString data
types...) TIA


A char is a char. A char* often is either a pointer to a single char or,
much more often, a pointer to a zero-terminated char array (AKA a C-style
string).

And so, let's bear in mind that string functions such as concat operate on
strings not chars.

You can have a string that contains only one character (not counting the
terminating zero, if any).

You should be able to use std::string, like so:

#include <string>
#include <iostream>

int main()
{
using std::string;
using std::cout;

string str = "a";
char c = 'b';

str += c;
cout << str; // outputs "ab"
}

Regards,
Ben
Sep 19 '05 #3

P: n/a
The thing is, you can't realy.
If you have
char ch = 'a';
the only the you can do to cast it to char* is to copy it to a 2 byte
array as described below...

Sep 19 '05 #4

P: n/a
"Michael R. Copeland" <mr*****@cox.net> wrote in message
news:MP************************@news.west.cox.net. ..
How do I cast or promote a char variable to a char* variable? For
example, I want to use strcat to append a character to an existing
"string". (BTW, I'm not able to use STL string or CString data
types...) TIA


In addition ot the other comments, it seems very dangerous to cast a char to
a char *; the size of the char * is generally larger than the size of char.

You can do something like this:

char a = 'a';
char *b = &a;

However, if you wanted to use strcat, b is not null terminated and would
likely cause strcat to malfunction.

If a function such as strncat is available to you, you could do something
like

strncat(dest, b, 1);

which appends on character from 'b' to dest.
Sep 19 '05 #5

P: n/a
> The thing is, you can't realy.
If you have
char ch = 'a';
the only the you can do to cast it to char* is to copy it to a 2 byte
array as described below...


I don't see anything "below". That's my question - how _do_ I make
the cast so I can strcat (or wharever) the character?
Sep 20 '05 #6

P: n/a
"Michael R. Copeland" wrote:
The thing is, you can't realy.
If you have
char ch = 'a';
the only the you can do to cast it to char* is to copy it to a 2 byte
array as described below...


I don't see anything "below". That's my question - how _do_ I make
the cast so I can strcat (or wharever) the character?


As has been pointed out: You can't. Well, technically you can, but it doesn't do
you no good. strcat doesn't expect just a pointer. strcat expects a pointer
to an array and that array must be 0-terminted. A single character isn't 0
terminated, thus strcat will do fancy things.

All you need to do is set up a 2 element array, copy your character into
the first position, make sure the C-style string is 0-terminated and there
you go: strcat() does the right thing:

char ch = 'a';
char tmp[2];
tmp[0] = ch;
tmp[1] = '\0';
strcat( to_whatever, tmp );

--
Karl Heinz Buchegger
kb******@gascad.at
Sep 20 '05 #7

P: n/a

"Michael R. Copeland" <mr*****@cox.net> schrieb im Newsbeitrag
news:MP************************@news.west.cox.net. ..
How do I cast or promote a char variable to a char* variable? For
example, I want to use strcat to append a character to an existing
"string". (BTW, I'm not able to use STL string or CString data
types...) TIA


char some[128]="";
char add='a';

strcat(some, (char*)&((short)add<<8));
Sep 20 '05 #8

P: n/a
Gernot Frisch wrote:

"Michael R. Copeland" <mr*****@cox.net> schrieb im Newsbeitrag
news:MP************************@news.west.cox.net. ..
How do I cast or promote a char variable to a char* variable? For
example, I want to use strcat to append a character to an existing
"string". (BTW, I'm not able to use STL string or CString data
types...) TIA


char some[128]="";
char add='a';

strcat(some, (char*)&((short)add<<8));


To say it with the words of my compiler:
error C2102: '&' requires l-value

--
Karl Heinz Buchegger
kb******@gascad.at
Sep 20 '05 #9

P: n/a
"Karl Heinz Buchegger" wrote:
Gernot Frisch wrote:
"Michael R. Copeland" schrieb:
How do I cast or promote a char variable to a char* variable? For
example, I want to use strcat to append a character to an existing
"string". (BTW, I'm not able to use STL string or CString data
types...) TIA


char some[128]="";
char add='a';

strcat(some, (char*)&((short)add<<8));


To say it with the words of my compiler:
error C2102: '&' requires l-value


Even if one changes the code so that the construct is an
l-value and will compile, it still won't work on a little-endian
system (such as windows), in which 0x6100 looks like "0x0061"
in memory, which, as a string, is "\0a", which is the empty string.

It works if you get rid of the shift:

#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
int main()
{
char Text[128] = "fjwux";
cout << "Original string = " << Text << endl;
short Add = (static_cast<short>('a')); // 0x0061, little-endian 0x6100
strcat(Text, reinterpret_cast<char*>(&Add)); // concatenates "a/0"
cout << "Add = " << std::hex << std::showbase << Add << endl;
cout << "Combined string = " << Text << endl;
return 0;
}

That prints:

Original string = fjwux
Add = 0x61
Combined string = fjwuxa

It works (at least, on little-endian systems), but it's very
messy and brittle.

Much better is:

std::string Text ("fjwux");
Text += 'a';
cout << Text << endl;

--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
Sep 21 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.