Pointer equivalent to array notation

This is a discussion on Pointer equivalent to array notation within the C Programming forums, part of the General Programming Boards category; Hi all, I bought the 2-volume C for Dummies books a long time ago and now a few years later, ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    2

    Pointer equivalent to array notation

    Hi all,

    I bought the 2-volume C for Dummies books a long time ago and now a few years later, I have decided I am going to thoroughly learn pointers once and for all! Anyway, I've been going through the pointer chapter and haven't been having too much trouble so far, but then one of the exercises at the end of the section has me a bit stumped.

    The exercise asks to match the array notation to its pointer notation equivalent.

    So:

    array[0] is equivalent to *a
    array[1] is equivalent to *(a+1)
    array[x] is equivalent to *(a+x)

    No problems with these, but then:

    array[i+2] is equivalent to *(i+2)

    Where did this come from? Why wouldn't it be something like:

    array[i+2] is equivalent to *(a+(i+2))

    Or is this some sort of typo in the book?

    Thanks!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Looks like a typo, but direct quotes could help my 1% doubt.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Those Dummies books are full of errors; I wouldn't be surprised if you've found one . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    2
    Thanks so much!

    I'm at the point now where I'm pretty sure it's an error. Here's the exercise from the book exactly:

    1. Match the array notation to its pointer notation equivalent:

    A. array[0]
    B. array[1]
    C. array[i+2]
    D. array [x]

    1. *a
    2. *(a+1)
    3. *(i+2)
    4. *(a+x)

    Answers:

    1. A-1, B-2, C-3, D-4

    Yep, it was just that simple to match up. None of them were switched around at all. I just couldn't figure out the whole i+2 thing. I wonder if it was supposed to be:

    C. array[2]

    Answer: *(a+2)

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Hmm. My take:
    Code:
    array[0]     == *array          
    array[1]     == *(array + 1)    
    array[i + 2] == *(array + i + 2)
    array[x]     == *(array + x)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-15-2009, 08:38 AM
  2. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 07:33 AM
  3. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2007, 11:26 PM
  4. Replies: 10
    Last Post: 11-06-2005, 08:29 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21