Thread: Pass by Reference with a pointer to char

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

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    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.,
    char str[] = "hello world";
    here, str can safely be an array of non-const char since it is not a string literal, unlike:
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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