Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > difference b/w char arr[], & char *pointer

Reply
Thread Tools

difference b/w char arr[], & char *pointer

 
 
rajm2019@gmail.com
Guest
Posts: n/a
 
      06-09-2007
hi all,

i want to know that what is the actual difference b/w the character
array
& character pointer.then how u will get the addrees of a char array[]

char str[]="be silent like u"
char *p1="be eloquent r u"
char *p2;
p2=str;

but printing the p2 will give me the str

 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      06-09-2007
<> wrote in message
news: oups.com...
> hi all,
>
> i want to know that what is the actual difference b/w the character
> array
> & character pointer.then how u will get the addrees of a char array[]
>
> char str[]="be silent like u"
> char *p1="be eloquent r u"
> char *p2;
> p2=str;
>
> but printing the p2 will give me the str


Most print routines (std::cout, printf, etc...) treat a char pointer as a
c-style string and do indeed print the string. One simple work aroiund is
to cast it to some other pointer type and output that. Such as:

std::cout << reinterpret_cast<int*>( p2 );
static_cast may(?) also work.
You can treat str the same way in output to output the address.


 
Reply With Quote
 
 
 
 
peter koch
Guest
Posts: n/a
 
      06-09-2007
On 9 Jun., 08:52, rajm2...@gmail.com wrote:
> hi all,
>
> i want to know that what is the actual difference b/w the character
> array
> & character pointer.then how u will get the addrees of a char array[]
>
> char str[]="be silent like u"

This is an array to 17 chars (remember the leading zero). You can
change the variable, e.g. str[0] = 'B';.
> char *p1="be eloquent r u"

This is a const pointer to an array of char. Do not be fooled about
the missing const.
> char *p2;
> p2=str;
>
> but printing the p2 will give me the str

You can of course print both str and p2 because of the way arrays
decay to a pointer to the first element.

/Peter

 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      06-09-2007
On 9 Jun, 08:15, peter koch <peter.koch.lar...@gmail.com> wrote:
> On 9 Jun., 08:52, rajm2...@gmail.com wrote:> hi all,
> > char str[]="be silent like u"

>
> This is an array to 17 chars (remember the leading zero).


trailing, not leading.

> > char *p1="be eloquent r u"

>
> This is a const pointer to an array of char. Do not be fooled about
> the missing const.


It's a non-const pointer to const char.

To the OP: The declaration above is deprecated. You should use

const char *p1="be eloquent r u";

for string literals.

Gavin Deane

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-09-2007
On Jun 9, 8:52 am, rajm2...@gmail.com wrote:

> i want to know that what is the actual difference b/w the
> character array & character pointer.


One is an array, and the other is a pointer. What more
difference do you want?

> then how u will get the addrees of a char array[]


By taking its address, using the & operator.

> char str[]="be silent like u"


This defines an array, initialized with the characters in the
string literal.

> char *p1="be eloquent r u"


This defines both a pointer and an (unnamed) array---a string
literal is an unnamed unmodifiable array. The pointer is
initialized with the address of the first character in the
unnamed array.

Note that this is not really correct C++. It's supported for
historical reasons, but you really should write:

char const* p1 = "be eloquent r u" ;

> char *p2;
> p2=str;


Here, there is the same implicit conversion as in the previous
initialization: an array (str) is implicitly converted to the
address of its first member.

> but printing the p2 will give me the str


That's because of a long standing tradition that functions
(most, anyway) receiving a pointer to a char will treat it as
the address of the first element of an array of char, and will
process all of the following elements as well, up to but not
including a final '\0'.

In practice, the case almost never comes up in C++, because we
rarely if ever declare arrays of char, nor use pointer to char.

--
James Kanze (Gabi Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      06-10-2007
On Jun 9, 8:15 pm, Gavin Deane <deane_ga...@hotmail.com> wrote:
> On 9 Jun, 08:15, peter koch <peter.koch.lar...@gmail.com> wrote:
> > On 9 Jun., 08:52, rajm2...@gmail.com wrote:> hi all,

>
> > > char *p1="be eloquent r u"

>
> > This is a const pointer to an array of char.
> > Do not be fooled about the missing const.

>
> It's a non-const pointer to const char.


To clarify, the type of p1 is 'pointer to char',
i.e. non-const pointer to non-const char.

Despite this, the chars it is pointing at are const.

 
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
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
char *fred; char * fred; char *fred; any difference? Ben Pfaff C Programming 5 01-17-2004 07:37 PM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57