gcc - C++ Difference Between const char* and const char[] -
i read question on difference between:
const char* and
const char[] where while, though arrays syntactic sugar pointers. bugging me, have pice of code similar following:
namespace somenamespace { const char* str = { 'b', 'l', 'a', 'h' }; } i get, error: scaler object 'str' requires 1 element in initializer. so, tried this:
namespace somenamespace { const char str[] = { 'b', 'l', 'a', 'h' }; } it worked, @ first thought may have fact operation applied when const char*, , gcc never fan of operations being performed outside function (which bad practice anyway), error not seem suggest so. in:
void func() { const char* str = { 'b', 'l', 'a', 'h' }; } it compiles fine expected. have idea why so?
x86_64/i686-nacl-gcc 4(.1.4?) pepper 19 tool - chain (basically gcc).
first off, doesn't make difference if try use compound initialization @ namespace scope or in function: neither should work! when write
char const* str = ...; you got pointer sequence of chars can, e.g., initialized string literal. in case, chars located somewhere else pointer. on other hand, when write
char const str[] = ...; you define array of chars. size of array determined number of elements on right side and, e.g., becomes 4 example { 'b', 'l', 'a', 'h' }. if used, e.g., "blah" instead size would, of course, 5. elements of array copied location str defined in case.
note char const x[] can equivalent writing char const* x in contexts: when declare function argument, char const x[] same char const*.
Comments
Post a Comment