473,441 Members | 1,440 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,441 software developers and data experts.

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

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

Jun 9 '07 #1
5 4560
<ra******@gmail.comwrote in message
news:11**********************@a26g2000pre.googlegr 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.
Jun 9 '07 #2
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

Jun 9 '07 #3
On 9 Jun, 08:15, peter koch <peter.koch.lar...@gmail.comwrote:
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

Jun 9 '07 #4
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: ja*********@gmail.com
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

Jun 9 '07 #5
On Jun 9, 8:15 pm, Gavin Deane <deane_ga...@hotmail.comwrote:
On 9 Jun, 08:15, peter koch <peter.koch.lar...@gmail.comwrote:
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.

Jun 10 '07 #6

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

Similar topics

10
by: David | last post by:
what's the differences between: int main(int argc,char* argv){ ... } and: int main(int argc,char** argv){ ...
79
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The...
10
by: jeniffer | last post by:
char *a="this is a string"; char a="this is a string"; first is a pointer while second is an array.Tell me more differences
10
by: yogeshmk | last post by:
I need to break a longer string (with strtok()) and store the smaller strings in an array. since the number of small strings is not fixed/known, I cannot use a declaration like char *format, so I...
3
by: gevadas | last post by:
sample program #include <iostream> #include <vector> using namespace std; int find(char*& value,char** arr,int size) { for(int i = 0;i < size;i++)
21
by: arnuld | last post by:
int main() { const char* arr = {"bjarne", "stroustrup", "c++"}; char* parr = &arr; } this gives an error: $ g++ test.cpp test.cpp: In function 'int main()': test.cpp:4: error: cannot...
45
by: anto frank | last post by:
hi friends, is ther any difference in array in c and array in c++?
24
by: Logan | last post by:
Why (I) works but (II) gives segmentation error? (I) int main(int argc, char *argv) { printf("%s", argv); } (II) int main(int argc, char *argv) { printf("%s", argv);
12
by: Jrdman | last post by:
hi, in some function prototypes why they use const char* instead of const char what's the need for const here is? if we must use const and why they don't use char *const instead because it's...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.