String and const char *

This is a discussion on String and const char * within the C++ Programming forums, part of the General Programming Boards category; I am reading some documents about c++ and it says:In c++ string is equals to const char *.Is this true? ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    61

    String and const char *

    I am reading some documents about c++ and it says:In c++ string is equals to const char *.Is this true?
    Because i compile this code:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main() {
    	const char * p="ppppp";
    	string s="aaaaaaaa";
    	s+="eeeee";
    	s=p;
    	p=s;//gives error:error C2440: '=' : cannot convert from 'std::string' to 'const char *'	
    }
    Isn't string equals to const char *?If equals why does this error occur and why does compiler give permission to change value of string s after initializition?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    It is probably referring to a string in general, not the C++ string class. In C++, there is a standard class called string that should be used for string processing. That is different than const char* and has different properties. One property is that you can assign a C style string (char* or const char*) to it, and a copy is made and stored in the string. That is why you can change the string.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    A string is a sequence of characters.

    A C-style string is sequence of characters stored in a char array with the last character being a '\0'.

    A C++ string is a special C++ type called 'string' that was created to make handling a sequence of characters much easier than in C.
    Last edited by 7stud; 03-04-2006 at 07:04 PM.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    And a string literal ( "hello" ) ought to be of type const char[<length of string + 1>], which is implicitely convertible to const char*, but is in practice usually char[<length of string + 1>] for backwards compatibility. The standard might even require this.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > p=s;//gives error:error C2440: '='
    You could do
    p = s.c_str();

    This allows you to view the contents of your C++ string as a const char *

    Note the const is very important, since if you try and cast that const away as a means of modifying the C++ string through the back door, then you're going to break something.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    why does compiler give permission to change value of string s after initializition?
    Because the C++ string type is defined to allow you to do that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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