Results 1 to 2 of 2

Thread: Creating a pointer to one element in an array

  1. #1

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238

    Creating a pointer to one element in an array

    I have an array of objects:
    Obj *ObjectDatabase = new Obj[10];
    Obj *curObject;

    At some point, i want to make curObject point to a single Obj in the array, like curObject = myObject[2], only that doesn't work.
    please let me know how to do this.
    thanks

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The [] operator automatically derefences.

    You can do
    curObject = &ObjectDatabase[2];
    or (I prefer it, although it is harder to read for a newbie)
    curObject = ObjectDatabase+2;
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width