Using a pointer as an array?

Should I use:
char* x = "asdasdasd";
or
char x[] = "asdasdasd";
to store an array?
In your case, there is absolutely no different between the two. You cannot allocate the array dynamically (using "new" and "delete") with the second one however...
If you're asking about C++, the answer is neither. Use std::string x = "asdasdasd"; to store a character string.

Your first example is an error (formerly a deprecated conversion), which, if corrected, would have been written const char* x = "asdasdasd";. This line creates a pointer called x, which points at the first letter 'a' of a nameless static array of 10 unmodifiable char.

Your second example creates an array called x and fills it with 10 modifiable characters (nine letters and a null)
Last edited on
Topic archived. No new replies allowed.