Thread: a list of structures

  1. #1
    My diaper's full....... stevey's Avatar
    Join Date
    Nov 2001
    Posts
    746

    a list of structures

    i am trying to reproduce something like this standard textbook code-

    ptr=malloc(record_count*sizeof(char))
    p=ptr;
    for (count=0;count<record_count)
    *p++=ptr

    but what i want is to "save" in memory a list of structures and then reproduce them later, ive got this so far -


    RECORD_DATA *stock_rec,*s;

    stock_rec=malloc(sizeof(RECORD_DATA));

    s=malloc(record_count*sizeof(RECORD_DATA));

    stock_rec=s;
    while(something)
    {
    stock_rec= (a function returns a pointer to data struct of type RECORD_DATA )

    stock_rec++;
    }

    i thought this would save the stock_rec one after each other in memory, but it doesn't

    all i want to do is save a list of structures but i'm struggling, can anybody help ????? i dont want an array, cos i may not know how big it needs to be
    Steve

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Code:
    RECORD_DATA *stock_rec,*s;
    
    // This is not neccisary
    // stock_rec=malloc(sizeof(RECORD_DATA));
    
    // Okay, now s is an array of RECORD_DATA
    s=malloc(record_count*sizeof(RECORD_DATA));
    
    // stock_rec is the variable we use to step through the array
    stock_rec=s;
    
    while(something)
    {
    
    // This is the problem, you are just changing the value of stock_rec.  Instead you need to change the value of what it is pointing at.
    //stock_rec= (a function returns a pointer to data struct of type 
    RECORD_DATA )
    
    // There are two ways to do this...
    changeRec (stock_rec); // example below
    
    // orr...
    *stock_rec = (a function that returns a RECORD_DATA [not a pointer])
    
    stock_rec++;
    }
    The first way, passing the pointer to the record in a function, is considered the better way, because you don't want a function to return structures which take a lot of memory.
    Code:
    void changeRec (RECORD_DATA * p)
    {
     p -> info = 0;
     p -> artist = 'M';
     p -> foo = bar;
     // etc...
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What exactly are you trying to do? Basicly make a dynamic array of structures?

    struct mystruct *myArray;
    myArray = malloc( sizeof(struct mystruct) * numberOfElements );

    myArray[x].somedata = blah;

    Or are you trying to make a linked list?

    struct mystruct *myList;

    myList = newNode( NULL );
    myList = newNode( myList );

    where 'newNode' is:
    Code:
    struct mystruct * newNode( struct mystruct * n )
    {
        struct mystruct *temp;
    
        temp = malloc( sizeof( struct mystruct );
        // fill data into temp, or not
    
        temp->next = n;
        return temp;
    }
    //edit: Ah, you beat me to it. Dangit.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    My diaper's full....... stevey's Avatar
    Join Date
    Nov 2001
    Posts
    746

    thanx guys

    Many thanx to QuestionC and Quzah for their very illuminating responces.

    *stock_rec = (a function that returns a RECORD_DATA [not a pointer])
    stock_rec++;

    is what i had in my tiny mind, i can see what i was doing wrong now thanx,
    i'm also coding my prog with a linked list and an array also for practice, so i'm much the wiser

    regards
    steve
    Steve

  5. #5
    My diaper's full....... stevey's Avatar
    Join Date
    Nov 2001
    Posts
    746

    QuestionC

    is this what you meant ???? it works anyway !!

    s=malloc(record_count*sizeof(RECORD_DATA));

    stock_rec=s;

    while(......)
    {
    p=search_file(part_no);

    changeRec(p);stock_rec++ ;

    }

    //after the processing, regurgitate list -

    stock_rec=s; // set stock_rec back to "beginning"
    // (last stock_rec set to 999999)

    while(strcmp(stock_rec->key,"999999")!=0)
    {

    printf("key=%6s \n",stock_rec->key);
    stock_rec++;
    }


    void changeRec(RECORD_DATA *p)
    {

    strcpy(stock_rec->key,p->key);
    strcpy(stock_rec->desc,p->desc);
    stock_rec->supp_code=p->supp_code;
    stock_rec->free_stock=p->free_stock;
    stock_rec->min_stock=p->min_stock;
    strcpy(stock_rec->movement_date,p-> movement_date);
    stock_rec->price=p->price;

    }

    any comments ????
    Steve

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Well, my rule of thumb is...
    if it works, then it's right

    But it's still good practice not to use global variables, like you appear to be doing. I suggest changing the changeRec function so that you pass it both, a pointer to p, -and- a pointer to stock_rec (and not use stock_rec as global).
    Callou collei we'll code the way
    Of prime numbers and pings!

  7. #7
    My diaper's full....... stevey's Avatar
    Join Date
    Nov 2001
    Posts
    746
    oh oh i always tend to use global variables !!!

    i'll try not to in future !!!
    Steve

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. urgent please please..
    By peter_hii in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2006, 06:35 AM
  2. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  3. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM