Thread: Help with a function (ptr_explode)

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    9

    Help with a function (ptr_explode)

    Im trying to make a function that will split the words (like perl's split and php's explode), but im getting a couple of errors which I don't understand.

    Errors:
    Code:
    $ gcc -W -Wall ptr_explode.c -o ptr_explode
    ptr_explode.c: In function `ptr_explode':
    ptr_explode.c:25: warning: `return' with a value, in function returning void
    ptr_explode.c: In function `main':
    ptr_explode.c:33: error: void value not ignored as it ought to be
    ptr_explode.c:36: warning: passing arg 1 of `puts' makes pointer from integer without a cast

    Code:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void ptr_explode(char *ptr) 
    {
        //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;
        char     words[128][256];
     
        result = strtok(string, " ");
        while (result != NULL) {
            strcpy(words[ntokens++], result);
            result = strtok(NULL, " ");
        }
        
        // puts(words[1]);
        
        return words;
    }
    
    int main(void)
    {
        char *ptr = "a pointer with a buch of words un fun funf un";
        char *exploded;
     
        exploded = ptr_explode(ptr);
     
        // supposed to print the 2nd word ("pointer")
        puts(exploded[1]);
     
        return 1;
    }
    And here's the original code that works (and compiles without a single error) which im trying to make into a function:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        char *ptr = "a pointer with a buch of words un fun funf unf ";
        int wtf = strlen(ptr);
        char string[wtf];
        strcpy(string, ptr);
        char *result = NULL;
    
        int  ntokens = 0;
        char words[256][256];
        result = strtok(string, " ");
        while (result != NULL) {
            strcpy(words[ntokens++], result);
            result = strtok(NULL, " ");
        }
        // prits the second and third word ("pointer", "width")
        puts(words[1]);
        puts(words[2]);
        return 0;
    }
    Last edited by phatsam; 03-11-2006 at 09:04 AM.

  2. #2
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    The datatype void can't return a value or be used in expressions.

    In other words, change void ptr_explode(char *ptr)

    to something like

    Code:
    char ptr_explode(char *ptr)

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    If I remember correctly, the reason why void functions can't return values or be used in expressions is because they emulate procedures (ie functions that don't return values) from other programming languages. I say emulate because C only has functions, not procedures.
    Last edited by cdalten; 03-11-2006 at 09:11 AM.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    9
    Yeah I've tried that, but that gives me
    Code:
    $ gcc -W -Wall ptr_explode.c -o ptr_explode
    ptr_explode.c: In function `ptr_explode':
    ptr_explode.c:25: warning: return makes integer from pointer without a cast
    ptr_explode.c:25: warning: function returns address of local variable
    ptr_explode.c: In function `main':
    ptr_explode.c:33: warning: assignment makes pointer from integer without a cast
    and Segmentation faults

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Did you try something like

    char * ptr_explode(char *ptr)

  6. #6
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    ptr_explode.c:25: warning: function returns address of local variable


    Oh sweet lord, I recognize the error message. I'm just going to shut up and let one of the regulars give you the other half of the answer.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to think about what the function does exactly. It appears to populate a 2D array with words. Thus, you need a way to return this 2D array. Now, any local variable is destroyed when its function ends. That means you can't use a local 2D array like you're doing. You'll need to dynamically allocated space for an array, allocate a line for each word, fill that word, and move to the next. When you're all done, you'll need to return a pointer to that.
    Code:
    char **splodie( char * );
    There's a prototype to work with. Have fun.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM