Traversing through an array to find link between elements

This is a discussion on Traversing through an array to find link between elements within the C Programming forums, part of the General Programming Boards category; The following code piece is supposed to take a string of a length between 1 and 80 chars, and divide ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    9

    Traversing through an array to find link between elements

    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.

    edit: cleared out a source of misinterpretation.
    Last edited by duckdace; 10-17-2010 at 11:19 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If every string is "a certain length", then every string must have the same number of 16 char blocks for it's content.

    So all you need an array of pointers to strings, and give each string a base address in your pointer array. So if you had say 4 blocks for each string, then you could remove the base address + 4 blocks, to eliminate a string's blocks.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    9
    Yeah, I see how that can be misunderstood. Actually, the strings vary in length, and one string can occupy one as well as five blocks. Edited first post. Sorry about that.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, second verse, "same as the first", as they say - well similar, anyway.

    You make a struct with a pointer and an int. The pointer has the address of the base of the first block, and the int has the number of blocks the string is stored into.

    Which may not help at all if the strings are not stored in contiguous blocks of memory.

    Hmmmm!

    The only thing I can think of atm is to put each strings blocks addresses, into a sub array (or linked list), and keep that structure up to date with additions and deletions of the strings and blocks.

    What way has the instructor suggested for doing this?

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    9
    Well. One hint we got was to use a variable in struct meta(never mentioned the type), to identify which datablocks that are reserved for this message. Perhaps I'm approaching this from the wrong angle... I feel lost.
    Last edited by duckdace; 10-17-2010 at 01:55 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    My disclaimer: I have never done this, and am a hobby coder, not a pro.

    I would use an array of structs which each hold two (maybe just one depending on the logic specifics), with the pointer to the starting block address.

    Now block one would start at array[1].start*, and array[2].start* would have the starting address of the second block. See what I mean 1 is in 1, and 2nd is in 2, etc.

    If you make an example of the program, and post it, we can get down to specifics. It may not be what you teacher had in mind though, since I'm not positive just what that was.

    But I bet it'll work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to find the last value in an array
    By mkanimozhi in forum C++ Programming
    Replies: 11
    Last Post: 12-08-2009, 10:15 PM
  2. Shifting elements in an Array
    By mmarab in forum C Programming
    Replies: 5
    Last Post: 12-10-2007, 12:11 PM
  3. Replies: 3
    Last Post: 06-09-2006, 10:53 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21