Thread: Are these two string initialization methods same?

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    76

    Are these two string initialization methods same?

    Are these both same methods (initializing const char)?
    1:
    Code:
    char c[] = "Hello";
    2:
    Code:
    char *c="Hello";

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    No they're not the same.

    One is an array, the other is a pointer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The first declares c to be an array of char, and initialises its content to "Hello". Since the number of elements of c was not explicitly specified, it follows that of the initialiser, i.e., c is an array of six char. The contents of c are modifiable, although of course you cannot assign to c itself since c is an array.

    The second declares c to be a pointer to char, then initialises c to point to the first character of the string literal "Hello". Note that this means that the code is likely to be poorly written: c should have been declared as a pointer to const char instead so as to avoid accidentally attempting to modify a string literal, which would result in undefined behaviour. However, you can assign to c itself since c is a non-const pointer.
    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

  4. #4
    Registered User
    Join Date
    Jan 2014
    Posts
    76
    I didn't get the meaning of these two lines: 1: " you cannot assign to c itself " & 2: you can assign to c itself since c is a non-const pointer

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by gaurav#
    I didn't get the meaning of these two lines: 1: " you cannot assign to c itself "
    Try to compile and observe the error message:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char c[] = "Hello";
        char d[] = "World";
        c = d;
        printf("%s\n", c);
        return 0;
    }
    Quote Originally Posted by gaurav#
    2: you can assign to c itself since c is a non-const pointer
    Compile and run:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char *c = "Hello";
        char d[] = "World";
        c = d;
        printf("%s\n", c);
        return 0;
    }
    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. Accessor Methods vs Jump to the code | String const vs String
    By marcoesteves in forum C++ Programming
    Replies: 1
    Last Post: 07-14-2014, 04:25 AM
  2. String Declaration and Initialization Question
    By limblet2 in forum C Programming
    Replies: 3
    Last Post: 03-06-2009, 01:46 AM
  3. initialization of char array (c style string)
    By manzoor in forum C++ Programming
    Replies: 1
    Last Post: 09-24-2008, 06:29 AM
  4. string methods hard time
    By Micko in forum C++ Programming
    Replies: 2
    Last Post: 08-07-2006, 10:00 AM
  5. String methods
    By MethodMan in forum C Programming
    Replies: 4
    Last Post: 03-10-2002, 11:39 AM

Tags for this Thread