Quick query about (surprise!) pointers

Discussion in 'C' started by Tsukatu, Jul 30, 2007.

  1. Tsukatu

    Tsukatu New Member

    Jul 30, 2007
    Likes Received:
    Trophy Points:
    I realize this may have been answered thousands of times already, but after some quick searching through archives I was unable to find the answer I was looking for.

    I'm reading through a "Teach Yourself C in 21 Days" book (published '97), basically so I have a comprehensive learning experience in learning C++ and C#.
    It says, and I quote:
    And later on it has two questions:
    ...and the answer key says that it's perfectly fine.
    The very next question is:
    ...and the answer key says that this will make your computer explode like a hand grenade (though not quite in those words).

    So, if the question isn't obvious...
    Is *string1 really == string1[], or is the book lying to me?
    In both declarations ('char *string1' vs 'char string1[]'), 'string1 == &string1[0]' regardless, right? ...right?
    So how are they different?

    I feel like I'm taking crazy pills...

  2. DaWei

    DaWei New Member

    Dec 6, 2006
    Likes Received:
    Trophy Points:
    Semi-retired EE
    Texan now in Central NY
    Home Page:
    You cannot declare an empty array. You may declare what LOOKS like an empty array, if you initialize it, as in

    char string [] = "This is a string";

    That's a favor to you, from the compiler; the compiler can count the number of element that will be required.

    *string and string [] are equivalent and either may be used as an argument to a function. A pointer to an array and the name of the array are equivalent, but not the same. The standard requires that there be a means of converting from an array name to a pointer to the first element of that array. If you use an array name and if you use a pointer to an array, then examine the emitted machine code, you will see that there is one level of indirection difference. The compiler has provided that for you, pursuant to the requirements of the standard.

    I would suggest that you read the pointer tutorial referenced in my signature. It touches on that equivalency, but non-equality.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice