Troubles passing 2D arrays of char

This is a discussion on Troubles passing 2D arrays of char within the C Programming forums, part of the General Programming Boards category; well, take a little look in it: Code: #include <stdio.h> #include <string.h> void ptr_explode(char *ptr, char *results) { //char *ptr ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    22

    Troubles passing 2D arrays of char

    well, take a little look in it:
    Code:
    #include <stdio.h>
    #include <string.h>
    void ptr_explode(char *ptr, char *results) 
    {
        //char *ptr = "a pointer with a buch of words un fun funf unf ";
        
        /* first 'convert' the pointer into a string so that we can use strtok */ 
        int      ptrlen = strlen(ptr);
        char     string[ptrlen];
        strcpy(string, ptr);
    
        char     *result = NULL;
        int      ntokens = 0, aux;
        char     words[256][256];
        result = strtok(string, " ");
        while (result != NULL) {
            strcpy(words[ntokens++], result);
            result = strtok(NULL, " ");
        }
        for(aux=0;aux<=ntokens;aux++){
                                  strcpy(results, words[aux]);
                                  }
    }
    
    int main(void)
    {
        char *ptr = "a pointer with a buch of words un fun funf un";
        char *exploded;
        char words[256][256];
        int aux, aux1;
        for(aux=0;aux<=256;aux++){
                                  for(aux1=0;aux1<=256;aux1++){
                                                            words[aux][aux1]='\0';
                                                            }
                                  }
        ptr_explode(ptr, words);
    
        return 0;
    }
    i'm very dizzy with all of this, plz someone tell me what i did wrong!

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    char *ptr = "a pointer with a buch of words un fun funf un";
    That is a string literal, and can not be modified (as in strtok, which inserts NULL's at the delineators). Try;

    Code:
    char ptr[] = "a pointer with a buch of words un fun funf un";
    Also your function should be taking a pointer to a pointer

    Code:
    void ptr_explode(char *ptr, char **results)

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    22
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    void ptr_explode(char *ptr, char **results) 
    {
        //char *ptr = "a pointer with a buch of words un fun funf unf ";
        
        /* first 'convert' the pointer into a string so that we can use strtok */ 
        int      ptrlen = strlen(ptr);
        char     string[ptrlen];
        strcpy(string, ptr);
    
        char     *result = NULL;
        int      ntokens = 0, aux;
        char     words[256][256];
    
        result = strtok(string, " ");
        while (result != NULL) {
            strcpy(words[ntokens++], result);
            result = strtok(NULL, " ");
        }
        
        for(aux=0;aux<=ntokens;aux++){
         puts("1");
                                  strcpy(results[aux], words[aux]);
                                  puts("2");
                                  }
    }
    
    int main(void)
    {
        char *ptr = "a pointer with a buch of words un fun funf un";
        char **exploded;
        char words[256][256];
        int aux, aux1;
        
    
        ptr_explode(ptr, words);//it dont run this function why??
        for(aux1=0;aux1<=10;aux1++){
                                     puts(words[aux]);
                      }
    
        getche();
        return 0;
    }
    i've modified it a bit, take a look...

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Nevermind my comments.

    Code:
    int ptr_explode(char *ptr, char results[][256])

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    answer

    As was sayed replace :
    Code:
    void ptr_explode(char *ptr, char **results)
    with
    Code:
    void ptr_explode(char *ptr, char  results[][256])

    and replace
    Code:
    puts(words[aux]);
    with
    Code:
    puts(words[aux1]);



    It is alway a good idea to remove variables you don't use.
    like aux.


    The all code. should look like this :
    Code:
    void ptr_explode(char *ptr, char results[][256], int *pntokens) 
    {
        char     *result;
        char     string[strlen(ptr)];
    
    
        strcpy(string, ptr);
    
        *pntokens = 0;
        result = strtok(string, " ");
        while (result != NULL) {
            strcpy(results[(*pntokens)++], result);
            result = strtok(NULL, " ");
        }
    }
    
    int main(void)
    {
        char *ptr = "a pointer with a buch of words un fun funf un";
        char words[256][256];
        int  aux;
        int  ntokens = 0;    
    
        ptr_explode(ptr, words, &ntokens);
        for(aux=0 ; aux < ntokens ; aux++)
            puts(words[aux]);
    
        getche();
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    22
    thank u all!
    now i can proceed in my personal project of my conversor!!

    thx a lot!! and sorry for my newbie mistakes, and sorry too by my poor english

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Passing 2d arrays to functions
    By owi_just in forum C Programming
    Replies: 2
    Last Post: 05-06-2005, 05:22 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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