2

Possible Duplicate:
What is the difference between char s[] and char *s in C?
Difference between char a[]=“string”; char *p=“string”;

Firstly, i would like to ask where can i learn all the basics of char* and char[].

Most of the time i find myself struggling with how to compare and how to declare.

Example 1 :

   char *test = "hello world";

This will produce the following warning at compilation :

 warning: deprecated conversion from string constant to ‘char*’

Example 2 :

   vector<char*> test2;
   test2.push_back("hello world");

This will produce an error of copying a string.

So the solution i come up with is :

(is this correct?)

   vector<char*> test3;
   char *str1 = "hello world"
   test3.push_back(str1);

Thanks in advance! :)

============================================

Two good reads provided by people here :

What is the difference between char s[] and char *s?

Difference between char a[]="string"; char *p="string";

Community
  • 1
  • 1
mister
  • 3,303
  • 10
  • 31
  • 48

3 Answers3

6

Your question "where can i learn all the basics of char* and char[]," is probably too general, but you can try reading the C++ spec.

Fix example 1 by changing it to

char const *test = "hello world";

Fix example 2 by changing it to

vector<std::string> test2;
test2.push_back("hello world");

Or if you really want a vector of non-owning pointers to c-strings:

vector<char const *> test2;
test2.push_back("hello world");
bames53
  • 86,085
  • 15
  • 179
  • 244
  • Oops, you beat me to it... I [exceeded my limit on deletes](http://meta.stackexchange.com/q/98746/175984) for the day, so I'll just upvote yours. – Sergey Kalinichenko May 15 '12 at 21:03
3

You can learn a lot about char*/char[] in your favorite book on C (not on C++, because C++ provides much better facilities for representing strings than C does).

The declaration/assignment

char *test = "hello world";

should be

const char *test = "hello world";

because string literals are pointers to constants. If you want a vector of strings in C++, you do it like this:

std::vector<std::string> test2;
test2.push_back("hello world");

This works, because string literals can be implicitly converted to std::string.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Even C++ books go into that enough to get a feel of them. It's important to know, even if you are using C++. – chris May 15 '12 at 21:04
  • @chris You are right, knowing about `char*` is important to C++ programmers. But for C programmers that knowledge is vital :) – Sergey Kalinichenko May 15 '12 at 21:06
-1

So the thing to remember is the difference between a pointer to a value and the value itself.

The value itself is the literal value that a variable represents as stored in memory. A pointer is the address in memory that some value is stored at.

Pointers are useful because they allow you to access, change, and preserve changes in information across multiple functions / methods, which comes in handy when you realize you can only return one value from any function, but sometimes you want a lot more information than that.

The thing about arrays is that, while a lot of people don't realize this when first learning C++, they are pointers. A[0] isn't a variable, it's a pointer to a memory address. Arrays are handy because when declaring an array, you segment off a portion of memory reserved for use for that array. This is useful because it allows you to access the values stored in that block of memory very very quickly.

So really, there's not that much difference between declaring a pointer (char*) and an array (char[]) other than that the pointer will refer to a single location in memory while the array will refer to set of contiguous locations in memory.

To learn more about pointers and how to use them properly, visit http://www.cplusplus.com/doc/tutorial/pointers/ .

MattS
  • 717
  • 2
  • 7
  • 22
  • The thing about arrays is that, while a lot of people believe this, arrays are not pointers. Recommended reading: http://stackoverflow.com/q/1641957/46642 and http://stackoverflow.com/q/4810664/46642 – R. Martinho Fernandes May 15 '12 at 21:11
  • From my link to cplusplus.com : "In fact, the identifier of an array is equivalent to the address of its first element, as a pointer is equivalent to the address of the first element that it points to, so in fact they are the same concept." There are differences between arrays and pointers, obviously, but the point is that they are both just references to memory locations. – MattS May 15 '12 at 21:28
  • cplusplus.com is known for [sporting several mistakes](http://programmers.stackexchange.com/questions/88241/whats-wrong-with-cplusplus-com). This is just one of them. – R. Martinho Fernandes May 15 '12 at 21:38
  • Huh, I've never heard of problems with cplusplus.com before. It's generally been very helpful to me in all my c++ related endeavours. Thanks for the heads up. I still think the general idea that pointers and arrays are similar (if not the same) can be very helpful, though, especially for people who're having trouble understanding what exactly pointers do. – MattS May 15 '12 at 21:44