Thread: struct arrays

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    55

    struct arrays

    This is just a basic question. I've created a struct that needs to create mutliple instances, but of a constantly changing number.

    Grab number of objects from memory.
    Code:
    struct _NumOfObjects
    {
    	unsigned short Max;
    	unsigned short Num;	
    };
    _NumOfObjects * NumOfObjects = ( _NumOfObjects * ) OTNumAddy;
    Code:
    struct _Lengths
    {
    	float AX;
        float TrueBX;
        float ExtremeBX;
    	float AY;
        float TrueBY;
        float ExtremeBY;
    
        float dist; 
        float mx, my, mz;
    } Length[NumOfObjects->Num];
    I need to create an instance for each seperate object, but I get this error:

    error C2057: expected constant expression
    error C2466: cannot allocate an array of constant size 0

    Is there a way to do this without a constant? The num of objects changes quite frequently.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Are you using C or C++? In C++, you'd use a vector and specify the size when you create it. In C you could use malloc to dynamically allocate the array, or maybe use variable length arrays.

    BTW, technically you're not allowed to use any name that starts with an underscore followed by a capital letter.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    55
    I'm using C++ and I'm not very familiar with C functions =/

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by silentkarma View Post
    Code:
    _NumOfObjects * NumOfObjects = ( _NumOfObjects * ) OTNumAddy;
    WTF?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I'm using C++
    Ok, then:
    Code:
    std::vector<_Lengths> Length(NumOfObjects->Num);
    You have to make sure NumOfObjects is valid at that point, so I'm not sure making it global is a good idea, but hopefully you just did that as an example (or because you thought it was necessary for a struct -- it's not).

  6. #6
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    I think the problem is (like robwhit said), this:
    Code:
    _NumOfObjects * NumOfObjects = ( _NumOfObjects * ) OTNumAddy;
    What do you mean by it?
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    55
    Quote Originally Posted by g4j31a5 View Post
    I think the problem is (like robwhit said), this:
    Code:
    _NumOfObjects * NumOfObjects = ( _NumOfObjects * ) OTNumAddy;
    What do you mean by it?
    This is assigning a static memory address to this struct. This is for a game. I hope this makes sense. I'm doing calculations on each object and decided to use struct arrays. I figured this would be the easiest way because I can easily iterate through objects.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What is OTNumAddy?

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    55
    Quote Originally Posted by Daved View Post
    What is OTNumAddy?
    const unsigned long OTNumAddy = 0x4BB206E2;

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So NumOfObjects->Num is the value found at 0x4BB206E4. Do you have any idea how the compiler should know what will be at this address when you execute the code?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM