By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,799 Members | 1,356 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,799 IT Pros & Developers. It's quick & easy.

confusion with pointers andd arrays

P: n/a
I recently purchased Stephen Davis's book C++ For Dummies, and came
across a simple declaration that I don't understand the workings of.
About 1/5 way through the book I see

char* szString = "Randy";

I was under the impression that char* szString should make a pointer
variable called szString. So why is the value set to "Randy" when from
what I've learned it should be set to &someVariable (the location of
some variable). This is located in the pointers chapter in a section
called "Expanding pointer operations to a string" if that is any help.
Thank's in advance for your time and your help.

Keegan Hernandez

Jan 3 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 2 Jan 2007 20:36:45 -0800 in comp.lang.c++, "quadraticformula"
<da***********@gmail.comwrote,
>char* szString = "Randy";

I was under the impression that char* szString should make a pointer
variable called szString. So why is the value set to "Randy" when from
what I've learned it should be set to &someVariable
It's being initialized to &someConstant where the constant is "Randy".
The & is implicit. It probably ought to be type (char const *) instead,
strictly speaking.

Jan 3 '07 #2

P: n/a
quadraticformula wrote:
char* szString = "Randy";
I was under the impression that char* szString should make a pointer
variable called szString. So why is the value set to "Randy" when from
what I've learned it should be set to &someVariable (the location of
The effect of the code is approximately this:

const char compiler_generated_hidden_variable []=
{ 'R', 'a', 'n', 'd', 'y', '\o' };
char * szString= & compiler_generated_hidden_variable [0];

And the constness violation is silently ignored in this special case because
of compatibility with old C code. A good compiler will be able to emit a
warning, though.

Better ways of write that line of code are:

const char szString []= "Randy";
const char * szString= "Randy";
char szString []= "Randy";

Depending on what you want to do with szString.

--
Salu2
Jan 3 '07 #3

P: n/a
quadraticformula wrote:
I recently purchased Stephen Davis's book C++ For Dummies, and came
across a simple declaration that I don't understand the workings of.
About 1/5 way through the book I see

char* szString = "Randy";

I was under the impression that char* szString should make a pointer
variable called szString. So why is the value set to "Randy" when from
what I've learned it should be set to &someVariable (the location of
some variable). This is located in the pointers chapter in a section
called "Expanding pointer operations to a string" if that is any help.
Thank's in advance for your time and your help.
In most situations, the name of an array decays to a pointer to its
first element. For example:

void f(const char *);
const char str[] = "Randy";

Now, f(str) is the same as f(&str[0]). Similarly, when you use a string
literal in the same way, it decays to a pointer to the first element:

f("Randy")

calls f, and passes the address of the letter 'R' in the string literal.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Jan 5 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.