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.

11-08-2001

The V.

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.

11-09-2001

gliptic

The '[]' and '*' are exactly the same if you didn't know that.

11-09-2001

Brown Drake

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".

11-09-2001

gliptic

Yes, and that makes them the same. You can however use the '[]' to define pointers to multidimension arrays like this:

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;

11-12-2001

Unregistered

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?

11-12-2001

Unregistered

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"

11-16-2001

Unregistered

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...

11-16-2001

The V.

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 *.

11-16-2001

Nick

I tested some c code with

Code:

void f(void)
{
const char s[] = "Hello";
}

and

Code:

void f(void)
{
const char* s = "Hello";
}

The assembly output on gcc is totally different.
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)

11-17-2001

doubleanti

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...