Thread: Passing pointers to arrays of char arrays

  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.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You just pass it as it is:
    Code:
    void somefunction(char **tokens)
    {
    }
    
    int main(void)
    {
      char **tokens;
    
      somefunction(tokens);
      return 0;
    }
    How does your main() function currently know how many tokens there are? I don't see your stringTokeniser() function returning a count or anything.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122
    To find the number of tokens, i just do...

    Code:
    count = sizeof tokens
    count--;
    For some reason, the last token is always junk!!!! hence the reason why the count-- to ignore the last token!!!!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    'sizeof tokens' just gives you the size of the object itself. In this case, the size of a pointer to a pointer.

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

  5. #5
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122
    OK!!! ...But in main() to count the number of tokens, I simply do this.....

    Code:
    int count;
    for (count = 0; count < sizeof tokens / sizeof ** tokens; count++); /* COUNT NUMBER OF TOKENS */
    count--; /* NUMBER OF TOKENS FOUND! */

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's nice. It's still wrong, and it still gives you the size of the pointer itself, not the size of what it points to. You can't ever use sizeof on pointer and get the size of all the objects it points to. It doesn't work that way.
    Code:
    char c, *ptr, **ptrptr;
    size_t x;
    
    x = sizeof c; /* 1 */
    x = sizeof ptr; /* 2 */
    x = sizeof ptrptr; /* 3 */
    1 - Will only ever give you the size of one character.
    2 - Will only ever give you the size of one pointer to a character.
    3 - Will only ever give you the size of one pointer to a pointer to a character.

    All you get is the size of that type. You don't get the size of everything it poitns to. That only works with arrays (which are not pointers), that just happen to be in the same scope as your usage of the sizeof operator. If you pass the array to a function, and try it then, it won't give you the size of the array. It'll give you a size of a pointer. Nothing more, nothing less. So no matter what you think is happening in main when you do that, it's not. If they happen to be the same number, it's purely by luck.


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

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