a quick Q??

M

Mike Wahler

batee5a said:
what is the difference between char* and char[]?????

In the context of a function parameter, nothing,
they both mean 'pointer to char'

In a declaration other than a function parameter,
The former signifies a pointer, the latter is
a syntax error.

-Mike
 
B

Bob Hairgrove

batee5a said:
what is the difference between char* and char[]?????

In the context of a function parameter, nothing,
they both mean 'pointer to char'

In a declaration other than a function parameter,
The former signifies a pointer, the latter is
a syntax error.

Hmmm...

I think the OP was thinking about this:
const char msg[] = "Hello";
or this:
char buffer[256];

as opposed to this:
const char * msg = "Hello";
or this:
char * buffer;

(rather than "char[]", which doesn't compile, of course).

You can cast an array to a pointer, but not the other way around.
You can perform "pointer arithmetic" on a pointer, but not on an
array, i.e.:
char buffer[256];
char * pc = buffer; //OK: pc == &(buffer[0])
++pc; // OK: pc == &(buffer[1])
++buffer; // error
 
P

Phlip

char* p;
char a[10];

p is of type pointer to char. It points somewhere but no one know where; it
shouldn't be used until that problem is taken care of.

a is a pointer to an array of 10 char in auto storage

'a' is the name of an array of 10 chars, in storage provided by its context.

'a' decays into the address of its first item easily. But this template (if
I type it right) treats 'a' as a first-class object:

template<size_t len> size_t extent(char (&a)[len]) { return len; }

The (&a) part forces the function to pass 'a' by reference, not address of
its first item.
 
M

Mike Wahler

Bob Hairgrove said:
batee5a said:
what is the difference between char* and char[]?????

In the context of a function parameter, nothing,
they both mean 'pointer to char'

In a declaration other than a function parameter,
The former signifies a pointer, the latter is
a syntax error.

Hmmm...

I think the OP was thinking about this:
const char msg[] = "Hello";
or this:
char buffer[256];

I try to stay away from assumptions, and only address
the actual content of the query. I have however, been
known to speculate from time to time. :)

-Mike
 
O

osmium

batee5a said:
what is the difference between char* and char[]?????

Consider:

char* p;
char a[10];

p is of type pointer to char. It points somewhere but no one know where; it
shouldn't be used until that problem is taken care of.

a is a pointer to an array of 10 char in auto storage

This distinction is lost when either of these is passed as an argument to a
function, the function will always see a pointer to char. It is said
"arrays decay to pointers" ... or some such.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top