Thread: Error

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    17

    Unhappy Error

    Hi all,

    I justed started C programming, after 1.5 year PHP programming. I tried to make a explode function just like in PHP, which exlodes a string with a given separator. Example:

    char = explode ("bla bla", " "); // should return:

    char[0][3] = "bla";
    char[1][3] = "bla";

    But it doesn't.

    Source:

    Code:
    #include <string.h>
    #include <stdio.h>
    
    char **func_explode (char *str, char *sep) {
      printf ("str: '%s' sep: '%s'",str,sep);
      char **newstr = malloc(sizeof(str));
      newstr[0] = strtok (str, sep);
      char *x = malloc (1024*4);
      x = strtok(NULL, sep);
      int i = 1;
      while (x != NULL) {
        newstr[i] = x;
        char *x = strtok(NULL, sep);
        i++;
      }
      return newstr;
    }
    
    int main (int argc, char **argv) {
      char *zin = "Hoi, ik ben bart enzo :D";
      char *sep = " ";
      char nieuwzin = **func_explode (zin, sep);
      return 0;
    }
    This is the error it says:

    ~/C$ make explode
    cc -O2 -fno-strict-aliasing -pipe explode.c -o explode
    ~/C$ ./explode
    Bus error: 10 (core dumped)
    ~/C$


    Can anyone please help me?

    Thank you!
    Bart S.

  2. #2
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    How many times are you going to declare x?

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    17
    Lol, fixed that, thanks, but the error is on line 7...

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    17
    Edit:

    If I don't declare x in the for loop, I get an error @ compiling:
    ~/C$ make explode
    cc -O2 -fno-strict-aliasing -pipe explode.c -o explode
    explode.c: In function `func_explode':
    explode.c:13: warning: initialization makes integer from pointer without a cast
    ~/C$

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    can you post the updated code?

    this line looks wrong to me but i dont know C:
    char nieuwzin = **func_explode (zin, sep);

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    17
    Sure I can:

    Code:
    #include <string.h>
    #include <stdio.h>
    
    char **func_explode (char *str, char *sep) {
      char **newstr = malloc(sizeof(str));
      char *x = malloc (1024*4);
      int i = 1;
      newstr[0] = strtok (str, sep);
      x = strtok(NULL, sep);
      while (x != NULL) {
        newstr[i] = x;
        char x = strtok(NULL, sep);
        i++;
      }
      return newstr;
    }
    
    int main (int argc, char **argv) {
      char *zin = "Hoi, ik ben bart enzo :D";
      char *sep = " ";
      char nieuwzin = **func_explode (zin, sep);
      return 0;
    }

    ~/C$ make explode
    cc -O2 -fno-strict-aliasing -pipe explode.c -o explode
    explode.c: In function `func_explode':
    explode.c:12: warning: initialization makes integer from pointer without a cast
    ~/C$ ./explode
    Bus error: 10 (core dumped)
    ~/C$
    Last edited by bartz; 10-11-2006 at 08:21 AM.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i still dont like the look of this line:
    char nieuwzin = **func_explode (zin, sep);

    is that legal C? try:
    char **nieuwzin = func_explode (zin, sep);

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    17
    l think you're right, fixed that:

    Code:
    #include <string.h>
    #include <stdio.h>
    
    char **func_explode (char *str, char *sep) {
      char **newstr = malloc(sizeof(str));
      char *x = malloc (1024*4);
      int i = 1;
      newstr[0] = strtok (str, sep);
      x = strtok(NULL, sep);
      while (x != NULL) {
        newstr[i] = x;
        char x = strtok(NULL, sep);
        i++;
      }
      return newstr;
    }
    
    int main (int argc, char **argv) {
      char *zin = "Hoi, ik ben bart enzo :D";
      char *sep = " ";
      char **nieuwzin = func_explode (zin, sep);
      return 0;
    }
    Still errors:

    ~/C$ make explode
    cc -O2 -fno-strict-aliasing -pipe explode.c -o explode
    explode.c: In function `func_explode':
    explode.c:12: warning: initialization makes integer from pointer without a cast
    ~/C$ ./explode
    Bus error: 10 (core dumped)
    ~/C$

  9. #9
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    char * func_explode (char *str, char sep)
    {
        int i;
        char * newstr;
    
        printf("str: '%s' sep: '%c'\n", str, sep);
    
        newstr = malloc(strlen(str) + 2);
    
        if (newstr == NULL)
        {
            /* error */
            return(NULL);
        }
    
        memset(newstr, 0, strlen(str) + 2);
        strcpy(newstr, str);
    
        for (i = 0; i < strlen(str); i++)
        {
            if (newstr[i] == sep) newstr[i] = '\0';
        }
    
        return(newstr);
    }
    
    int main (int argc, char **argv)
    {
        char *zin = "Hoi, ik ben bart enzo :D";
        char sep = ' ';
        char * nieuwzin;
        char * ptr = NULL;
    
        nieuwzin = func_explode(zin, sep);
    
        ptr = nieuwzin;
    
        if (ptr != NULL)
        {
            for ( ptr = nieuwzin;
                  *ptr != '\0';
                  ptr += strlen(ptr) + 1 )
            {
                printf("%s\n", ptr);
            }
    
            free(nieuwzin);
        }
    
        return(0);
    }

  10. #10
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    you define x as 'char *' then later in the while loop you define it as 'char x'

  11. #11
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    In your code ...

    Code:
    char **newstr = malloc(sizeof(str));
    You don't want sizeof(str) that will be 4 bytes (on a 32-bit O/S) you want the length of the string + 1 (for the terminating char).

    Code:
    char **newstr = malloc(strlen(str) + 1);
    You are also using strtok() which changes the contents of the string passed to it in its 1st argument, but you are passing it a string constant (which the compiler might decide to make constant!).

    Code:
      char *x = malloc (1024*4);
    You set x to point to the memory you've malloc'd (ignoring the fact you haven't checked if the malloc worked)

    Code:
    x = strtok(NULL, sep);
    Then you overwrite x, thus leaking the memory you malloc'd/
    Last edited by SKeane; 10-11-2006 at 08:36 AM.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    17
    Thank you very much!
    What did I do wrong?

    Another question:
    If I want to ask a password, how can I make sure it's printed as *'s or it isn't printed at all?

  13. #13
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    On a PC, Unix box?

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    17
    Quote Originally Posted by nadroj
    you define x as 'char *' then later in the while loop you define it as 'char x'
    What's the difference?

  15. #15
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Quote Originally Posted by SKeane
    In your code ...
    Code:
    char **newstr = malloc(strlen(str) + 1);
    Actually, that'd probably be more like :

    Code:
    char** newstr = malloc((strlen(str) +1) * sizeof(char))
    Although, if that's actually what he intends to, I don't know why he has a char** instead of a char*...
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 10:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM
Website Security Test