Casting char to char*

Discussion in 'C++' started by Michael R. Copeland, Sep 19, 2005.

  1. 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
     
    Michael R. Copeland, Sep 19, 2005
    #1
    1. Advertising

  2. Michael R. Copeland

    Jim Langston Guest

    "Michael R. Copeland" <> wrote in message
    news:...
    > 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.
     
    Jim Langston, Sep 19, 2005
    #2
    1. Advertising

  3. Michael R. Copeland

    benben Guest

    > 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
     
    benben, Sep 19, 2005
    #3
  4. Michael R. Copeland

    Guest

    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, 2005
    #4
  5. "Michael R. Copeland" <> wrote in message
    news:...
    > 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.
     
    Bill Thompson, Sep 19, 2005
    #5
  6. > 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?
     
    Michael R. Copeland, Sep 20, 2005
    #6
  7. "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
     
    Karl Heinz Buchegger, Sep 20, 2005
    #7
  8. "Michael R. Copeland" <> schrieb im Newsbeitrag
    news:...
    > 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));
     
    Gernot Frisch, Sep 20, 2005
    #8
  9. Gernot Frisch wrote:
    >
    > "Michael R. Copeland" <> schrieb im Newsbeitrag
    > news:...
    > > 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
     
    Karl Heinz Buchegger, Sep 20, 2005
    #9
  10. "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
     
    Robbie Hatley, Sep 21, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lovecreatesbeauty
    Replies:
    1
    Views:
    1,056
    Ian Collins
    May 9, 2006
  2. Xavier Roche

    Aliasing bug when casting char[] into char* ?

    Xavier Roche, Feb 6, 2007, in forum: C Programming
    Replies:
    4
    Views:
    412
    Peter Nilsson
    Feb 7, 2007
  3. Xavier Roche

    Aliasing bug when casting char[] into char* ?

    Xavier Roche, Feb 6, 2007, in forum: C Programming
    Replies:
    1
    Views:
    338
    Xavier Roche
    Feb 7, 2007
  4. Michael R. Copeland

    Casting char to char*

    Michael R. Copeland, Sep 19, 2005, in forum: C++
    Replies:
    10
    Views:
    459
    Default User
    Sep 22, 2005
  5. Alex Vinokur
    Replies:
    9
    Views:
    797
    James Kanze
    Oct 13, 2008
Loading...

Share This Page