473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

a quick Q??

what is the difference between char* and char[]?????
Jul 19 '05 #1
5 1436
"batee5a" <ra*******@hotmail.com> wrote in message
news:5c************************@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

Jul 19 '05 #2
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.
Jul 19 '05 #3
On Sun, 19 Oct 2003 20:20:54 GMT, "Mike Wahler"
<mk******@mkwahler.net> wrote:
"batee5a" <ra*******@hotmail.com> wrote in message
news:5c************************@posting.google.co m...
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
No**********@Home.com
Jul 19 '05 #4
> 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
Jul 19 '05 #5

"Bob Hairgrove" <wouldnt_you_like@to_know.com> wrote in message
news:3f**************@news.webshuttle.ch...
On Sun, 19 Oct 2003 20:20:54 GMT, "Mike Wahler"
<mk******@mkwahler.net> wrote:
"batee5a" <ra*******@hotmail.com> wrote in message
news:5c************************@posting.google.co m...
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
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.