confusion with pointers andd arrays

  • Thread starter quadraticformula
  • Start date
Q

quadraticformula

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
 
D

David Harmon

On 2 Jan 2007 20:36:45 -0800 in comp.lang.c++, "quadraticformula"
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.
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

quadraticformula said:
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.
 
P

Pete Becker

quadraticformula said:
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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top