Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - char* vs. string*

If I have a pointer that points to a string variable array of chars, is there a difference between typing:

char *name = "name";

And,

string name = "name";
like image 604
Simplicity Avatar asked Jan 28 '11 09:01

Simplicity


People also ask

Is string the same as char * in C?

The C programming language distinguishes character constants from string constants by using quotation marks in the following manner: 'c' is the character c, while "c" is a string of length 1 consisting of the single character c. Why is this distinction made?

Is char * the same as string?

The difference between a string and a char* is that the char* is just a pointer to the sequence. This approach of manipulating strings is based on the C programming language and is the native way in which strings are encoded in C++.

What is the difference between char * A and char * A?

1) “char a” represents a character variable and “char a[1]” represents a char array of size 1. 2) If we print value of char a, we get ASCII value of the character (if %d is used). And if we print value of char a[1], we get address of the only element in array.

Why is a char * a string?

char *A is a character pointer. it's another way of initializing an array of characters, which is what a string is. char A, on the other hand, is a single char. it can't be more than one char.


2 Answers

Yes, there’s a difference. Mainly because you can modify your string but you cannot modify your first version – but the C++ compiler won’t even warn you that this is forbidden if you try.

So always use the second version.

If you need to use a char pointer for whatever reason, make it const:

char const* str = "name";

Now, if you try to modify the contents of str, the compiler will forbid this (correctly). You should also push the warning level of your compiler up a notch: then it will warn that your first code (i.e. char* str = "name") is legal but deprecated.

like image 77
Konrad Rudolph Avatar answered Sep 21 '22 00:09

Konrad Rudolph


For starters, you probably want to change

string *name = "name";

to read

string name = "name";

The first version won't compile, because a string* and a char* are fundamentally different types.

The difference between a string and a char* is that the char* is just a pointer to the sequence. This approach of manipulating strings is based on the C programming language and is the native way in which strings are encoded in C++. C strings are a bit tricky to work with - you need to be sure to allocate space for them properly, to avoid walking off the end of the buffer they occupy, to put them in mutable memory to avoid segmentation faults, etc. The main functions for manipulating them are in <cstring>. Most C++ programmers advise against the use of C-style strings, as they are inherently harder to work with, but they are still supported both for backwards compatibility and as a "lowest common denominator" to which low-level APIs can build off of.

A C++-style string is an object encapsulating a string. The details of its memory management are not visible to the user (though you can be guaranteed that all the memory is contiguous). It uses operator overloading to make some common operations like concatenation easier to use, and also supports several member functions designed to do high-level operations like searching, replacing, substrings, etc. They also are designed to interoperate with the STL algorithms, though C-style strings can do this as well.

In short, as a C++ programmer you are probably better off using the string type. It's safer and a bit easier to use. It's still good to know about C-style strings because you will certainly encounter them in your programming career, but it's probably best not to use them in your programs where string can also be used unless there's a compelling reason to do so.

like image 41
templatetypedef Avatar answered Sep 18 '22 00:09

templatetypedef