Thread: reference elements in a typedef array?

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    2

    reference elements in a typedef array?

    Hello,

    I'm trying to reference the elements of an array that is based on a typedef but not sure how to do it. VC is treating the index as the entire size of the array. For example:

    Code:
    typedef struct Struct1 {
      int var1;
      int var2;
    } Struct1Type;
    
    typedef Struct1Type ArrayOfStruct1Type[8];
    
    typedef struct Struct2 {
      int var3;
      ArrayOfStruct1Type var4;
    } Struct2Type;
    
    Struct2Type vars2;
    ArrayOfStruct1Type *arr=&vars2.var4;
    
    for (int i=0;i<8;i++) {
      int xy=arr[i]->var1;  // <<<<< doesn't work
    }
    How to I reference the elements in the array????

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You're asking for a pointer to the whole array. What you probably want is this:
    Code:
        Struct1Type *arr = vars2.var4;
    Also, the access should be like this:
    Code:
       int xy=arr[i].var1;
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    arr is a pointer to an ArrayOfStruct1Type. Therefore, arr[0] is an ArrayOfStruct1Type. arr[1] is another ArrayOfStruct1Type, which in this case does not exist. What you probably wanted was:
    Code:
    Struct1Type *arr = vars2.var4;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    Try this
    Code:
    int xy=arr[i].var1;

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    2
    Quote Originally Posted by oogabooga View Post
    You're asking for a pointer to the whole array. What you probably want is this:
    Code:
        Struct1Type *arr = vars2.var4;
    Also, the access should be like this:
    Code:
       int xy=arr[i].var1;
    Yep, Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-07-2011, 11:22 PM
  2. Assigning Array Elements By Reference
    By bhenderson in forum C Programming
    Replies: 8
    Last Post: 08-23-2009, 03:21 PM
  3. sort elements of 2D matrix by 1st column reference
    By cfdprogrammer in forum C Programming
    Replies: 12
    Last Post: 05-04-2009, 03:26 PM
  4. typedef cross reference? Help
    By manliz in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2006, 10:28 PM
  5. structs and elements by reference?
    By kybert in forum C++ Programming
    Replies: 20
    Last Post: 10-07-2002, 09:46 PM