7

I want to know the differences between char * and string. for example in this code:

char *a; string b; 

Can anyone help me please?

6
  • 1
    @DavidHeffernan The cat can eat biscuit, biscuit can't eat a cat. Commented Jun 18, 2013 at 13:15
  • @SuvP In extreme cases, the biscuit can come to the cat. Commented Jun 18, 2013 at 13:17
  • @SuvP Do you know what's common between them? Commented Jun 18, 2013 at 13:21
  • 1
    @MarounMaroun Both can lie around doing nothing? (I hope you were not expecting nethin technical :P ) Commented Jun 18, 2013 at 13:24
  • @SuvP No. They're both not a dinosaur. Commented Jun 18, 2013 at 13:24

5 Answers 5

16

Assuming you're referring to std::string, string is a standard library class modelling a string.

char* is just a pointer to a single char. In C and C++, various functions exist that will take a pointer to a single char as a parameter and will track along the memory until a 0 memory value is reached (often called the null terminator). In that way it models a string of characters; strlen is an example of a function (from the C standard library) that does this.

If you have a choice, use std::string as you don't have to concern yourself with memory.

Sign up to request clarification or add additional context in comments.

1 Comment

Since string is a class, which data-member of it, holds the text that we assign to string?
3

char* is a pointer to a primitive type: char

string is a first class object from the Standard Template Library that wraps a lot of functionality (like concatenating two strings) and makes it easier to work with.

2 very different entities!

Comments

0

if you are worrying about functionality, string is a functional char* i.e you need not to worry about space

char*

declaration/initialization: char* str = "Use";

appending: XXX

finding length: strlen(str); //need to include <string.h> or create your own

string

declaration/initialization: string str = "Use";

appending: str += " This!"

finding length: str.length() //all in one header file

Comments

0

char* can also be a pointer to the 0 (first) place of an array of characters. It was used frequently in C, where the use of String is not supported.

3 Comments

if you declare char a pointer to an array of characters then your answer would be right but here its some thing else ,its a single character
even by being a single character, it is an array with one cell. *a refers to its value, while &a refers to its address within the memory.
i don't understand which array you are referring to, "logical" or "physical".
0

Its simple, char *a; declares a pointer 'a' of type char,it will point to a constant string or character arrays. String b; declares b as an object of string type.String here is a class which contains several string manipulation member functions(methods).You can look here for further details:http://www.cplusplus.com/reference/string/string/

One example program describing the string object and its member function is given below:

#include <iostream> #include <string> using namespace std; int main () { string str ("steve jobson"); cout <<"hello the name of steve jobs consists of"<< str.size() << " characters.\n"; return 0; } 

str is declared as string object and the member function size() is called to get the size of str.

Comments