Is there a difference between
const char * BOB = "bob";
and
const char BOB[] = "bob";
Is there a difference between
const char * BOB = "bob";
and
const char BOB[] = "bob";
im still a newbie *5 days of programming* but, im sure the top one will flag an error. Your declaring a cons char, by deffinition char's only hold 1 character. so assigning it to "BOB" is 3. Wont work. you have to use an array, BOB[4] = "bob"; 4 to hold the null, but i dont think you have to declare it manualy *not sure* but if its const you might as well.
I hope im write, and this helps.
c++->visualc++->directx->opengl->c++;
(it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)
Actually, they are in fact *mostly* equivalent.
const char * BOB = "Bob";
This will make a pointer on the stack. The pointer will point at a location in the program's data section which contains "Bob\0".
const char BOB[] = "Bob";
This will actually allocate 4 bytes (more specifically, 4*sizeof(const char)) on the stack, and it will copy "Bob\0" into it. I *think* this is how it works -- because it's a const, the compiler might optimize by making it point into the data section in place of allocating stack space. In other words, the compiler might treat the second situation exactly like the first.
There is a much larger contrast between these lines, though:
char * BOBptr = "Bob";
char BOBarr[] = "Bob";
These will behave differently. It's important, actually, to understand HOW they behave.
The '[]' and '*' are exactly the same if you didn't know that.
// Gliptic
An array name is, in practice, a pointer. So in char BOB[], BOB is a pointer to the first element of the array. In char* BOB, the pointer BOB points to the first character in "Bob".
Yes, and that makes them the same. You can however use the '[]' to define pointers to multidimension arrays like this:
char txt[][5];
This is a pointer to a two-dimensional array with five elements each.
// Gliptic
But, the difference is this:
char BOB[] = "Bob";
allocates space on the stack for 4 chars.
This:
char * BOB = "Bob";
allocates stack space for one pointer.
Further, there are differences in assignment:
This is perfectly legal, making one pointer an alias for another:
char * string1 = "The String";
char * string2 = string1;
This is not legal:
char * string1 = "The String";
char string2[] = string1;
So arrays and pointers are NOT identical. Further, this:
char array[2][2]
is NOT even close, internally, to this:
char ** array = new char* [2];
for (int i = 0; i < 2; i++) array[i] = new char[2];
The first one is stored as a pointer to the first element of the array (which is contiguous on the stack), the second is a pointer to an array of pointers to arrays, which are probably not contiguous, and are on the heap.
They are also not the same, typewise.
char BOB[] = "Bob";
is of type char[].
char * BOB = "Bob";
is of type char *.
Now, when a char * is needed (and all char[] and char* are passsed to functions as pointers), char[] will be automatically cast to a char * by the compiler. So, this works:
char array[] = "Bob";
char * pArray = array;
but this fails:
char * ptr = "Bob";
char array[] = ptr;
Last edited by The V.; 11-09-2001 at 11:32 AM.
So what would be the difference in regards to
const char * BOB = "bob"
and
const char BOB[] = "bob"
I guess another thing consider in this discussion is
const char * const BOB = "bob"
if it were declared globally vs. locally?
Is there a "better" choice for global vs. local?
Would you want to declare it static?
So what would be the difference in regards to
const char * BOB = "bob"
and
const char BOB[] = "bob"
if it were declared globally vs. locally?
Is there a "better" choice for global vs. local?
Would you want to declare it static?
I guess another thing consider in this discussion is
const char * const BOB = "bob"
Oops I stated that ambiguously
What I meant was which choice of declaration const char * const BOB = "bob" vs const char BOB[] = "bob" is better for local use, which declaration is better for global in terms of memory, performance, safety, etc...
They're equally good; if either had to have an edge, const char * might have a slight edge, depending on how the compiler optimized code; const char[] might, depending on compiler, allocate space for the whole string on the stack, which is unnecessary. When in doubt, use const char *.
I tested some c code with
andCode:void f(void) { const char s[] = "Hello"; }
The assembly output on gcc is totally different.Code:void f(void) { const char* s = "Hello"; }
I would go with
const char* s = "Hello";
Also keep in mind that when "passing arrays" the array automatically
decays into a pointer so
void f(const char s[]) is
the same as
void f(const char* s)
also, if you allocate via arrays it is still possible that your entire array is segmented... tho access seems as if it is concrete... whereas with pointers, the programmers conception of the memory is more exacting...
hasafraggin shizigishin oppashigger...