Thread: const char * vs const char []

  1. #1
    Unregistered
    Guest

    Question const char * vs const char []

    Is there a difference between
    const char * BOB = "bob";
    and
    const char BOB[] = "bob";

  2. #2
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    im still a newbie *5 days of programming* but, im sure the top one will flag an error. Your declaring a cons char, by deffinition char's only hold 1 character. so assigning it to "BOB" is 3. Wont work. you have to use an array, BOB[4] = "bob"; 4 to hold the null, but i dont think you have to declare it manualy *not sure* but if its const you might as well.

    I hope im write, and this helps.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Actually, they are in fact *mostly* equivalent.

    const char * BOB = "Bob";

    This will make a pointer on the stack. The pointer will point at a location in the program's data section which contains "Bob\0".

    const char BOB[] = "Bob";

    This will actually allocate 4 bytes (more specifically, 4*sizeof(const char)) on the stack, and it will copy "Bob\0" into it. I *think* this is how it works -- because it's a const, the compiler might optimize by making it point into the data section in place of allocating stack space. In other words, the compiler might treat the second situation exactly like the first.

    There is a much larger contrast between these lines, though:

    char * BOBptr = "Bob";
    char BOBarr[] = "Bob";

    These will behave differently. It's important, actually, to understand HOW they behave.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    The '[]' and '*' are exactly the same if you didn't know that.
    // Gliptic

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    An array name is, in practice, a pointer. So in char BOB[], BOB is a pointer to the first element of the array. In char* BOB, the pointer BOB points to the first character in "Bob".

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    Yes, and that makes them the same. You can however use the '[]' to define pointers to multidimension arrays like this:

    char txt[][5];

    This is a pointer to a two-dimensional array with five elements each.
    // Gliptic

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    But, the difference is this:

    char BOB[] = "Bob";

    allocates space on the stack for 4 chars.

    This:

    char * BOB = "Bob";

    allocates stack space for one pointer.

    Further, there are differences in assignment:

    This is perfectly legal, making one pointer an alias for another:

    char * string1 = "The String";
    char * string2 = string1;

    This is not legal:

    char * string1 = "The String";
    char string2[] = string1;

    So arrays and pointers are NOT identical. Further, this:

    char array[2][2]

    is NOT even close, internally, to this:

    char ** array = new char* [2];
    for (int i = 0; i < 2; i++) array[i] = new char[2];

    The first one is stored as a pointer to the first element of the array (which is contiguous on the stack), the second is a pointer to an array of pointers to arrays, which are probably not contiguous, and are on the heap.


    They are also not the same, typewise.

    char BOB[] = "Bob";

    is of type char[].

    char * BOB = "Bob";

    is of type char *.

    Now, when a char * is needed (and all char[] and char* are passsed to functions as pointers), char[] will be automatically cast to a char * by the compiler. So, this works:

    char array[] = "Bob";
    char * pArray = array;

    but this fails:

    char * ptr = "Bob";
    char array[] = ptr;
    Last edited by The V.; 11-09-2001 at 11:32 AM.

  8. #8
    Unregistered
    Guest

    Question

    So what would be the difference in regards to
    const char * BOB = "bob"
    and
    const char BOB[] = "bob"

    I guess another thing consider in this discussion is
    const char * const BOB = "bob"

    if it were declared globally vs. locally?

    Is there a "better" choice for global vs. local?

    Would you want to declare it static?

  9. #9
    Unregistered
    Guest

    Question

    So what would be the difference in regards to
    const char * BOB = "bob"
    and
    const char BOB[] = "bob"
    if it were declared globally vs. locally?

    Is there a "better" choice for global vs. local?

    Would you want to declare it static?

    I guess another thing consider in this discussion is
    const char * const BOB = "bob"

  10. #10
    Unregistered
    Guest
    Oops I stated that ambiguously

    What I meant was which choice of declaration const char * const BOB = "bob" vs const char BOB[] = "bob" is better for local use, which declaration is better for global in terms of memory, performance, safety, etc...

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    They're equally good; if either had to have an edge, const char * might have a slight edge, depending on how the compiler optimized code; const char[] might, depending on compiler, allocate space for the whole string on the stack, which is unnecessary. When in doubt, use const char *.

  12. #12
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    I tested some c code with

    Code:
    void f(void) 
    {
           const char s[] = "Hello"; 
    }
    and
    Code:
    void f(void)
    {
           const char* s = "Hello";
    }
    The assembly output on gcc is totally different.
    I would go with
    const char* s = "Hello";


    Also keep in mind that when "passing arrays" the array automatically
    decays into a pointer so
    void f(const char s[]) is
    the same as
    void f(const char* s)

  13. #13
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    also, if you allocate via arrays it is still possible that your entire array is segmented... tho access seems as if it is concrete... whereas with pointers, the programmers conception of the memory is more exacting...
    hasafraggin shizigishin oppashigger...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  4. Basic things I need to know
    By maxorator in forum C++ Programming
    Replies: 53
    Last Post: 10-15-2006, 04:39 PM
  5. Replies: 7
    Last Post: 06-16-2006, 09:23 PM