Thread: array pointer question

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67

    array pointer question

    Hey there. I'm new to here so bear with me. The question:

    If you have a declaration such as: int a[15];

    which experssion would be a pointer to the 3rd element?

    a+2
    or
    $(a+2)

    I'm a little confused. any info would be great

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
       int *ptr;
       ptr = a + 2;
       /* or */
       ptr = &a[2];
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Dave already posted the answer.

  4. #4
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by Hoser83
    that's what I thought too. this is actually a multiple choice assignment, and the the choices I were given are:

    a+2
    a[3]
    a+3
    $(a+2)

    do any of these make sense to you?

    I think Dave pegged it as the first choice there.

    The second choice looks like valid C, but is referencing the 4th element of the array, as array indexes (indices???) always start at 0.

    The third choice is also valid C, but again, references the 4th element.

    The last choice is not valid C.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Location
    North Liberty, IA
    Posts
    67
    oh, sorry guys. my bad. I didn't know if the

    experssion needed the ptr = a + 2 or if it was just a + 2.
    I got ya now.

    Thanks a lot for the help. I really appreciate it.

  6. #6
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    the expression to reference the 3rd element is "a+2", if you then want to assign that to "ptr" you will need to type "ptr=a+2".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer Array Question
    By d320 in forum C Programming
    Replies: 5
    Last Post: 07-11-2008, 11:51 PM
  2. pointer of array of class
    By 11moshiko11 in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2008, 09:58 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. A question between Array and Pointer
    By Unregistered in forum C++ Programming
    Replies: 16
    Last Post: 08-05-2002, 09:13 AM