Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What type is x in "char x[2];"?

Reply
Thread Tools

What type is x in "char x[2];"?

 
 
Daniel T.
Guest
Posts: n/a
 
      08-30-2004
In article <(E-Mail Removed)>,
cppaddict <(E-Mail Removed)> wrote:

> I thought that:
>
> char x[2];
>
> made x into a pointer-to-char.


It's more like a const-pointer-to-char. Note, I'm not saying it *is*
such a beast, but its very much *like* one.
 
Reply With Quote
 
 
 
 
Moritz Beller
Guest
Posts: n/a
 
      08-30-2004
On Mon, 30 Aug 2004 07:28:42 +0200
"William Payne" <(E-Mail Removed)> wrote:

> > Can you explain why the above two things work even though pointers
> > and arrays of different types?
> >

>
> It works because when an array is passed to a function it decays to a
> pointer to its first element.


Which also makes sizeof useless within the function. If you were to know
the array's elements, you'd need to add a parameter index:

foo(array,sizeof array/sizeof array[0]);

best regards
Moritz Beller
--
web http://www.4momo.de
mail momo dot beller at t-online dot de
gpgkey http://gpg.notlong.com
 
Reply With Quote
 
 
 
 
Thomas Matthews
Guest
Posts: n/a
 
      08-30-2004
cppaddict wrote:
> Can you explain why the above two things work even though pointers and
> arrays of different types?
>
> Thanks,
> cpp
>


Now would be a good time to become familiar with
Chris Torek's, "The Rule". Search the Web for
"Chris Torek The Rule". He explains in depth
the difference between an array and a pointer.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      08-30-2004

"cppaddict" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...

[Re array and pointer semantics and their similarites and differences]:

> Can you explain why the above two things work even though pointers and
> arrays of different types?


http://web.torek.net/torek/c/pa.html
(this is part of:
http://web.torek.net/torek/c/
... which has more good C info).

Also see:
http://pweb.netcom.com/~tjensen/ptr/pointers.htm
-Mike


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      08-30-2004
Karl Heinz Buchegger wrote:
>
> cppaddict wrote:
> >
> >
> > Can you explain why the above two things work even though pointers and
> > arrays of different types?

>
> You need to distinguish between what things *are* and how things are
> *used*
>
> If the name of an array is used without an indexing operation (*), then the
> name of the array decays into a pointer to its first element. But note:
> The name is still not a pointer, it is just used as if it were one.
>
> Heck. Even array indexing is defined in terms of pointer operations:
>
> char b[10];
>
> b[5] is identical to *(b+5) by definition
>
> (*) with sizeof beeing one exception that comes to my mind immediatly



Also the address-of operator (&).

That means that &b is NOT char** but is a pointer to array 2 of char.




Brian Rodenborn
 
Reply With Quote
 
cppaddict
Guest
Posts: n/a
 
      08-30-2004

>Now would be a good time to become familiar with
>Chris Torek's, "The Rule". Search the Web for
>"Chris Torek The Rule". He explains in depth
>the difference between an array and a pointer.


Thanks for the reference.... very interesting.

cpp

 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      08-30-2004
cppaddict <(E-Mail Removed)> wrote:
>
> the following prints b:
>
> int main() {
> char pbuf[2];


I guess you mean: char pbuf[3];

> pbuf[0] = 'a';
> pbuf[1] = 'b';
> pbuf[2] = '\0';
> std::cout << *(pbuf+1);
>
> return 0;
> }
>
> This makes it seem like a pointer.


When you use the name of an array in any expression other than
those listed below, it is as if you had written: &x[0].
The exceptions (I think..): sizeof x, &x, x++, ++x, --x, x--,
or where x is on the left-hand side of an assignment (=, +=, -=, etc.)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
type(d) != type(d.copy()) when type(d).issubclass(dict) kj Python 5 12-26-2010 06:48 PM
#define ALLOCIT(Type) ((Type*) malloc (sizeof (Type))) Yevgen Muntyan C Programming 10 02-13-2007 02:52 AM
type casting vs. type converting Toby VHDL 3 09-07-2005 01:42 PM
Re: Type casting- a larger type to a smaller type pete C Programming 4 04-02-2004 05:19 PM
Re: Type casting- a larger type to a smaller type heyo C Programming 3 04-01-2004 06:35 PM



Advertisments