Thread: Passing pointers to arrays of char arrays

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122

    Passing pointers to arrays of char arrays

    I have an array containing arrays of chars, these are tokens from a tokenised string!!! so basically, looks something like....

    - string one -
    - string two -
    - string three -

    and so on....

    Basically, I want to access this array of tokens in another function in my program!

    Code:
    char ** stringTokenizer(char * theData, char * theDelims){
      static char *Token[5];   /* ARRAY TO STORE POINTERS TO TOKENS */
      /* FUNCTION TOKENISES INPUT STRING HERE AND RETURNS THE TOKENS */
      return tokens;
    }
      
    main () {
      char ** tokens;  /* POINTER TO THE TOKENS */
      char mystring = "This is a test string";
      
      /* TOKENISE mystring DATA */
      tokens = stringTokenizer(mystring, " \r \n : ; #");
      
      /*
        T0 ACCESS THE TOKENS:
           tokens[0] = "This"
           tokens[1] = "is"
            tokens[2] = "a"
            tokens[3] = "test"
            tokens[4] = "string"
      */
      if (strcasecmp(tokens[0], "this") == 0) {  /* COMPARE FIRST TOKEN */
        printf("found \"this\" token\n");
      }
      
      anotherFunction(tokens);   /* CALL ANOTHER FUNCTION AND PASS THE TOKENS ARRAY TO IT! */
    }  
    
    anotherFunction(inToks) {
    
    }
    I know this is not the best description! ...But, it shows the fundamental concept of what goes on! ...I have a pointer to an array of pointers that point to individual char arrays (the tokens), I want to pass this pointer to the calling funciton so i can use the tokens in the other funciton!!!!!!

    I hope this makes some sense!!!!!!

    I want to be able to access the tokens (in the other function) by doing something similar to ;

    tokens[x] (will retrieve the token in position x)

    ...In the same way as i can in the main function!!!! ...Basically, I want to compare the tokens and find out what tokens contain certain data!!! (it is for university coursework, for a client server application, so packet data is the individual tokens and needs to be processed in different functions!!!!)



    Thanx for any help!!!!!!!! I hope it is understandable what i am trying to do!!!!!!!!
    Last edited by bobthebullet990; 03-30-2006 at 08:23 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Char arrays, pointers and if statements
    By rocketman50 in forum C Programming
    Replies: 5
    Last Post: 02-22-2009, 11:11 AM
  2. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  3. Troubles passing 2D arrays of char
    By paulovitorbal in forum C Programming
    Replies: 5
    Last Post: 04-05-2006, 06:37 AM
  4. use of pointers with char arrays
    By txp200 in forum C Programming
    Replies: 6
    Last Post: 05-12-2004, 06:52 PM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM