Re: char * vs char[]

Discussion in 'C++' started by John Harrison, Jun 25, 2003.

  1. "C Wood" <> wrote in message
    news:LDlKa.12831$...
    >
    > 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...
    >


    The second, because all you've declared is a pointer which points at a
    string literal, and string literals are read-only.

    Despite appearances the first doesn't involve a string literal, its a
    confusing short hand for this.

    char temp[] = {'p', 'r', 'e', 'p', 'e', 'n', 'd', '\0' };

    Obviously if you declare a non-const array, you can modify it.

    Your strcat is illegal in both cases, nothing to do with const, instead you
    are writing over memory you haven't allocated in any way.

    john
    John Harrison, Jun 25, 2003
    #1
    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:
    549
  2. wwj
    Replies:
    24
    Views:
    2,507
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    473
    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:
    580
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,043
    Ian Collins
    May 9, 2006
Loading...

Share This Page