confusion with pointers andd arrays

Discussion in 'C++' started by quadraticformula, Jan 3, 2007.

  1. 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
     
    quadraticformula, Jan 3, 2007
    #1
    1. Advertising

  2. quadraticformula

    David Harmon Guest

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


    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.
     
    David Harmon, Jan 3, 2007
    #2
    1. Advertising

  3. 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
     
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Jan 3, 2007
    #3
  4. quadraticformula

    Pete Becker Guest

    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)
     
    Pete Becker, Jan 5, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sebastian Millies

    BigDecimal API docs andd "reuse"

    Sebastian Millies, Jun 17, 2005, in forum: Java
    Replies:
    5
    Views:
    630
    Murray
    Jun 17, 2005
  2. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    387
  3. Replies:
    1
    Views:
    655
    -berlin.de
    Mar 28, 2005
  4. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    problem with memcpy and pointers/arrays confusion - again

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Mar 9, 2006, in forum: C Programming
    Replies:
    39
    Views:
    1,255
    Ben Pfaff
    Mar 21, 2006
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    681
Loading...

Share This Page