Thread: Hopefully, this is a tractable problem...

  1. #1
    Not bad at Crack Attack!
    Join Date
    Aug 2003
    Posts
    45

    Hopefully, this is a tractable problem...

    I have an array of pointers to N arrays (see my earlier post). I happy with the assignment of these things and I can access the elements of these arrays with ease. Fantastic.

    However, I am now required to modify these arrays, specifically I need to add elements to these arrays. This is done in the form of a function which has an integer, "i" say, passed to it. A random number is generated, call it x for example.

    Code:
    int *array[]={N0, .... , Ni, Nj, Nk, ... , NN};
    
    void add_to_array(int i)
    {
    int x;
    x=rand()
    
    /*How do I add x to array Ni???*/
    return;
    }
    If it helps I also have an array of length N where the i^th element comprises the number of elements in the i^th array. (If I refer to this particular array again, I will call it "noe"...)

    My current approach is to copy the existing array (array[i] using the code above), free it and then increase noe[i] by 1, then malloc a new Ni, fill it from the copy and stick x in at the end. However, I need to do this multiple times! I get a gut feeling that this will cause a problem... Using the most simplistic approach and calling the new array "new" then surely I cannot call the function more than once for fearing of really confusing things??!?



    I hope this is reasonably clear. I would rather not post the code I am actually working on or give too much of the context away because I am not looking to have code written for me... merely an approach suggested to help me on my way.

    Matt

  2. #2
    Not bad at Crack Attack!
    Join Date
    Aug 2003
    Posts
    45
    I am intruiged by "realloc". I've not come across it (I've only been C'ing - slowly - for approx 6 months) - just a few words to describe it or a link to a description maybe?

    Thanks for the help, I will have a look but it looks as though your suggestions will work - but I have to translate it all to my context of course...

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >a link to a description maybe?

    http://www.rt.com/man/realloc.3.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Not bad at Crack Attack!
    Join Date
    Aug 2003
    Posts
    45
    Originally posted by Matt13
    I am intruiged by "realloc". I've not come across it (I've only been C'ing - slowly - for approx 6 months) - just a few words to describe it or a link to a description maybe?

    Thanks for the help, I will have a look but it looks as though your suggestions will work - but I have to translate it all to my context of course...
    Thanks a lot, I have that bookmarked. I was familiar with malloc and calloc - discovering there is a realloc has made my day!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM