1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Traversing through an array to find link between elements

Discussion in 'C' started by duckdace, Oct 17, 2010.

  1. duckdace

    duckdace New Member

    Joined:
    Oct 9, 2010
    Messages:
    9
    Likes Received:
    1
    Trophy Points:
    0
    The following code piece is supposed to take a string of a length between 1 and 80 chars, and divide it into blocks of 16 chars each. This seems to work fine, but I would like to be able to keep track of which block belongs to which other, so that if I want to remove a whole message, every belonging block is cleared.

    You can see what my first idea was; to use the meta struct I already use for different purposes (not included in post, due to irrelevancy), and store a msg.msgID, but I'm unsure how to traverse through the blockarray, checking for this specific value.

    Can anyone give me some input, or even a better solution to my problem?

    Please ask if anything is unclear. My mind is kind of clouded atm, so there's a possibility that I'm not making my self sufficiantly understood.


    Code:
    
    #include/define whateverneeded
    
    char *block[16];
    int blockNmber;
    int msgNmber;
    
    struct meta {
      int msgID;
      
    };
    void newMSG(char *stringArray[])
    {
        struct meta msg;
        block[blockNmber] = malloc(16);
        int i;
        int j = 0;
        for (i=0;i<strlen(stringArray[1]);i++) {
          if (j >= 15 {
        blockNmber++;
        j = 0;
        block[blockNmber] = malloc(16);
          }
          else {
        
        block[blockNmber[j] = stringArray[1][i];
        j++;
          }
        }
        blockNmber++;
        msgNmber++;
        msg.msgID = msgNmber;
            
       
    }
    
    btw: I know there are some holes in my algorithm if i want to include a removeObj-function, but this will be fixed. instead of using blockNmber, i will check the array for free space, but I have not come that far yet.
     
  2. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
  3. duckdace

    duckdace New Member

    Joined:
    Oct 9, 2010
    Messages:
    9
    Likes Received:
    1
    Trophy Points:
    0
    Yeah, thank you for pointing that out. I'm still trying to figure out which forum is the most rewarding to be a part of, so it's not impossible that you'll find my queries other places than here on go4.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    The best option is binary search if the content is sorted or else you have no option but to go for a linear search
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    It's still not the best way to get help. If you're crossposting then you're requesting duplicate effort. So any time I put in to it might be completely wasted, which as a professional software engineer (who is easily capable of answering in way more depth than you ever knew existed, by the way) I find unacceptable.
     

Share This Page