Pointer to array of classes

This is a discussion on Pointer to array of classes within the C++ Programming forums, part of the General Programming Boards category; first off I have an array of classes with 50 elements i pass them to a function using a * ...

  1. #1
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    319

    Pointer to array of classes

    first off I have an array of classes with 50 elements i pass them to a function using a * instead of []'s. When I incriment my pointer do i need to incriment by sizeof(class) or 1?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    >> When I incriment my pointer do i need to incriment by sizeof(class) or 1?

    by one; the compiler automatically calculates the correct byte offset given the pointer type.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You can just use

    Code:
    pointername++;
    In most situations, the ++ operator just adds 1, but since you are working with a pointer, the compiler will automatically increment the memory address contained in your pointer by the right amount (nice feature).

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Hmm... Kind of reminds me of one of those stupid pet tricks you can do with pointers (namely, the addition of pointers using the subscript notation):
    Code:
    #include <iostream>
    int main()
    {
       int t[4] = { 7, 3, 4, 8 };
       for(int i = 0; i != 4; ++i)
          std::cout << i[t] << std::endl;
       return 0;
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    Yeah, because when you do subscripting you can write it out in its pointer form.

    Code:
    int array[6];
    ...
    std::cout << array[5] << std::endl;
    is the same as
    Code:
    int array[6];
    ...
    std::cout << *(array + (5*sizeof(int))) << std::endl;
    Since addition is commutative, you can rewrite that as
    Code:
    int array[6];
    ...
    std::cout << *((5*sizeof(int)) + array) << std::endl;
    which would be
    Code:
    int array[6];
    ...
    std::cout << 5[array] << std::endl;
    Very weird. When I first saw that I was like, "wtf?"

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Yeah, thats the explanation that I summed up in my cryptic little parenthetical above.
    It still just looks weird. "Yes, give me the arrayth element of 5, please..."
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

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. Dynamic pointer array in C
    By MacFromOK in forum Windows Programming
    Replies: 14
    Last Post: 04-09-2005, 06:14 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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