| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"batee5a" |
| Date: |
19 Oct 2003 02:18:22 PM |
| Object: |
a quick Q?? |
what is the difference between char* and char[]?????
.
|
|
| User: "Mike Wahler" |
|
| Title: Re: a quick Q?? |
19 Oct 2003 03:20:54 PM |
|
|
"batee5a" <rashrshha@hotmail.com> wrote in message
news:5c97113.0310191118.2ab92c5@posting.google.com...
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
.
|
|
|
| User: "Bob Hairgrove" |
|
| Title: Re: a quick Q?? |
19 Oct 2003 03:49:44 PM |
|
|
On Sun, 19 Oct 2003 20:20:54 GMT, "Mike Wahler"
<mkwahler@mkwahler.net> wrote:
"batee5a" <rashrshha@hotmail.com> wrote in message
news:5c97113.0310191118.2ab92c5@posting.google.com...
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
--
Bob Hairgrove
NoSpamPlease@Home.com
.
|
|
|
| User: "Mike Wahler" |
|
| Title: Re: a quick Q?? |
19 Oct 2003 03:58:37 PM |
|
|
"Bob Hairgrove" <wouldnt_you_like@to_know.com> wrote in message
news:3f92f661.3636328@news.webshuttle.ch...
On Sun, 19 Oct 2003 20:20:54 GMT, "Mike Wahler"
<mkwahler@mkwahler.net> wrote:
"batee5a" <rashrshha@hotmail.com> wrote in message
news:5c97113.0310191118.2ab92c5@posting.google.com...
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
.
|
|
|
|
|
|
| User: "osmium" |
|
| Title: Re: a quick Q?? |
19 Oct 2003 05:32:17 PM |
|
|
batee5a writes:
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.
.
|
|
|
| User: "Phlip" |
|
| Title: Re: a quick Q?? |
19 Oct 2003 03:53:52 PM |
|
|
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.
--
Phlip
http://www.c2.com/cgi/wiki?TestFirstUserInterfaces
.
|
|
|
|
|

|
Related Articles |
|
|