Re: char * vs char[]

Discussion in 'C++' started by Thomas Matthews, Jun 25, 2003.

  1. C Wood wrote:
    > Dear group,
    >
    > I forgot which declaration is constant. Intentions are:
    >
    > void some_func() {
    > char temp[] = "prepend"; /*This one*/
    > char *temp= "prepend"; /*Or this one*/
    > strcat(temp," before this");
    > }
    >
    > Thanks...
    >
    >


    In either case the literal text "prepend" is constant.
    Also, you can't have a variable declared with two
    different types.

    The first declaration declares an array of char.
    The array is initialized with the characters from
    the text literal. The size of the array is determined
    by the length of the literal.

    In the second declaration, you are declaring a pointer
    to char which initialized to point to the literal.
    The pointer can be modified. If the target of the
    pointer is modified, undefined behavor is invoked.

    The second declaration causes the literal to have
    a physical address. In the first declaration, the
    literal doesn't have to have a physical address;
    the literal can disappear after the declaration
    is executed.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Jun 25, 2003
    #1
    1. Advertising

  2. Thomas Matthews

    C Wood Guest

    "Thomas Matthews" <> wrote in message
    news:...
    : C Wood wrote:
    : In either case the literal text "prepend" is constant.
    : Also, you can't have a variable declared with two
    : different types.
    :
    : The first declaration declares an array of char.
    : The array is initialized with the characters from
    : the text literal. The size of the array is determined
    : by the length of the literal.
    :
    : In the second declaration, you are declaring a pointer
    : to char which initialized to point to the literal.
    : The pointer can be modified. If the target of the
    : pointer is modified, undefined behavor is invoked.
    :
    : The second declaration causes the literal to have
    : a physical address. In the first declaration, the
    : literal doesn't have to have a physical address;
    : the literal can disappear after the declaration
    : is executed.
    :
    I know, I tossed a fast sample using syntax I don't normally use and
    totally wrote trash uncompilable code.

    What I really wanted was a better way than this:

    void func() {
    char temp[8];
    temp[0] = 'P';
    temp[1] = 0;
    strcat(temp,"asdf");
    }

    Application is tossing in drive letters and such before filenames, in a
    clean and readable manner. Why worry about what works? It just annoying to
    do it the slow way, when it seems it could be preinitiailized. Probably
    worried about something that makes no difference anyways. Premature
    opt.............l.

    That way the temp would be:

    "Pasdf"

    Looks like the method:

    char temp[8] = {'P',0,0,0,0,0,0,0};

    is the best, or the way I've been doing it is fine also.

    Back to work it is then...........
     
    C Wood, Jun 25, 2003
    #2
    1. Advertising

  3. C Wood wrote:
    <snip>
    >
    > Looks like the method:
    >
    > char temp[8] = {'P',0,0,0,0,0,0,0};
    >
    > is the best, or the way I've been doing it is fine also.
    >
    > Back to work it is then...........


    Err...

    char temp[8] = "P";

    or

    char temp[8] = {'P', 0};

    Works just as well, except I can't guarantee that the rest of temp past
    the first 2 bytes will be initialized to anything useful (like nuls for
    example). Saves on all that typing of nuls at least.

    --
    Corey Murtagh
    The Electric Monk
    "Quidquid latine dictum sit, altum viditur!"
     
    Corey Murtagh, Jun 25, 2003
    #3
  4. Thomas Matthews

    Ron Natalie Guest

    "Corey Murtagh" <> wrote in message news:...

    >
    > char temp[8] = {'P', 0};
    >
    > Works just as well, except I can't guarantee that the rest of temp past
    > the first 2 bytes will be initialized to anything useful (like nuls for
    > example). Saves on all that typing of nuls at least.


    Actually all you need is
    char temp[8] = { 'P' };

    The other seven spots are default initialized (zero'd).
    If you specify fewer initializers than the size of the array, the rest are
    default initialized.
     
    Ron Natalie, Jun 25, 2003
    #4
  5. C Wood wrote:
    > "Gavin Deane" <> wrote in message
    > news:...
    > : "C Wood" <> wrote in message
    > news:XcmKa.12914$...
    > : Any reason why std::string doesn't work for you?
    > : GJD
    >
    > The reason is that compiler extensions call this function. Any
    > discussion of that is Off Topic here.
    >


    Here is an example of using string and some functions/methods that
    require char *:

    #include <fstream>
    #include <string>
    #include <cstdlib>
    using namespace std;

    const string file_name = "my_data.txt";
    int main(void)
    {
    ofstream my_file(file_name.c_str());
    if (my_file)
    {
    my_file << "Hello." << endl;
    cout << "File written." << endl;
    }
    else
    {
    cout << "Error opening file." << endl;
    }
    return EXIT_SUCCESS;
    }

    I would hope that in the next iteration of the standard,
    they allow std::string as a parameter to standard methods
    and functions that require char *.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Jun 27, 2003
    #5
    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. wwj
    Replies:
    7
    Views:
    558
  2. wwj
    Replies:
    24
    Views:
    2,520
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    480
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    588
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,058
    Ian Collins
    May 9, 2006
Loading...

Share This Page