Thread: Help with Structure

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    17

    Lightbulb Help with Structure

    I need to dynamically create a new Memo structure to hold memoized fib #'s

    I have two structures

    Code:
    typedef struct HugeInteger
    {
        //array to hold the digits of a huge integer
        int *digits;
    
    
        //number of digits in the huge integer
        int length;
    } HugeInteger;
    Code:
    typedef struct Memo
    {
        // dynamically allocated HugeInteger array to store fib #'s
        struct HugeInteger *F;
    
    
        // current length of this array
        int length;
    } Memo;
    So far my code for creating the new memo is the following..

    Code:
    Memo *createMemo(void){
    
    
        int i;
    
    
        Memo *memo;
        memo=(Memo*)malloc(sizeof(Memo));
    
    
        if(memo == NULL)
            panic("ERROR: out of memory in createMemo()\n");
    
    
            memo->F = (HugeInteger*)malloc(sizeof(HugeInteger)*INIT_MEMO_SIZE);
    
    
        if(memo->F == NULL)
            panic("ERROR: out of memory in createMemo()\n");
    
    
            memo->length = INIT_MEMO_SIZE;
    
    
        HugeInit(memo->F, 0);
        HugeInit(memo->F, 1);
    
    
        for(i=1; i<INIT_MEMO_SIZE; i++)
            {
                F->digits[i] == NULL;
            }
                F->length = 0;
    
        return memo;
    
    }
    I am having trouble with initializing the struct inside of the new Memo, I need the digit fields to null and the length field to 0 to indicate that F[i] has not yet been memoized...I have F->digits and F->length in the for loop but this just simply doesn't work...
    Please point me in the right direction so I can get this initialized...Thank you!

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Code:
    for(i=1; i<INIT_MEMO_SIZE; i++)
            {
                F->digits[i] == NULL;
            }
                F->length = 0;
    No, you have not F->length inside the loop, but I think that is correct and you did not wanted to write that "and F->length in the for loop" above.

    First of all, are you sure that the loop is running? Why it starts from 1 instead of 0? Place a printf("%d\n", i); to see how many times the loop is executed.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    I don't know what these functions do:

    Code:
    /* ... */
        HugeInit(memo->F, 0);
        HugeInit(memo->F, 1);
    The code to initialize *memo should look like this. Note that will be no instances of HugeInteger after doing this initialization. You'll need additional code to allocate and set the pointers and lengths for instances of HugeInteger.

    Code:
    /* ... */
        for(i=0; i < INIT_MEMO_SIZE; i++)
            memo->F[i] = NULL;
        memo->length = 0;
    Last edited by rcgldr; 06-14-2013 at 04:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to find number of structure members in a given structure?
    By bhaskarReddy in forum C Programming
    Replies: 4
    Last Post: 01-16-2012, 05:37 AM
  2. Replies: 4
    Last Post: 04-25-2010, 10:57 AM
  3. Replies: 1
    Last Post: 04-02-2009, 06:51 AM
  4. Replies: 9
    Last Post: 05-21-2007, 12:10 AM
  5. Replies: 4
    Last Post: 11-22-2006, 12:20 PM

Tags for this Thread