Pass by Reference with a pointer to char

This is a discussion on Pass by Reference with a pointer to char within the C Programming forums, part of the General Programming Boards category; Thanks everyone! I really appreciate the advice and assistance!...

  1. #16
    Registered User
    Join Date
    Oct 2008
    Posts
    11
    Thanks everyone! I really appreciate the advice and assistance!

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,746
    To be precise, a string literal is not a pointer, but an array. For example "hello" would be an array of 6 chars, including the null character that is automatically appended. Now, this is often only a pedantic difference since arrays are converted to pointers to their first element whenever necessary. One difference arises with the use of sizeof, e.g., sizeof("hello") is 6, but sizeof(char*) may well be 4 (or whatever is the size of a pointer to char).

    Another thing to note is that you can initialise a char array with a string literal, e.g.,
    Code:
    char str[] = "hello world";
    here, str can safely be an array of non-const char since it is not a string literal, unlike:
    Code:
    const char *ptr = "hello world";
    where ptr points to the first char of the string literal "hello world". So, it is perfectly fine to assign to str[0], but assigning to ptr[0] results in undefined behaviour.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. Speed test result
    By audinue in forum C Programming
    Replies: 4
    Last Post: 07-07-2008, 05:18 AM
  4. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  5. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21