Thread: domain split function

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    2

    domain split function

    I am new on C

    Please guide me how to fix the problem "Segmentation fault"

    Thank you



    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h> 
    
    #define BUF_SIZE 1024
    
    char *domainsplit(char *fdomain) {
       int strlen_buf;
       int i;
       int dot;
       char *domain;
       
       unsigned char tmp_buf[BUF_SIZE];
       
       strlen_buf = strlen(fdomain);
       
       tmp_buf[strlen_buf] = '\0';
       strcpy(tmp_buf, fdomain);  
       
       for (i = 0; i < strlen_buf; i++) {
              tmp_buf[i] = tolower(tmp_buf[i]);
       }
        
       dot = 0;
       
       for (i = strlen_buf - 1; i >= 0; i--)
              if ((tmp_buf[i] == '.') &&
                  ((dot > 0) ||
                   ((strlen_buf - i) > 3)) &&
                  (++dot >= 2))
                break;
                            
       sprintf(domain,"%s", (dot >= 2) ? (tmp_buf + i + 1) : tmp_buf);
       return domain;
    }
     
    int main(void) {
      char *txt;
      txt = domainsplit("43476347.www.blahblah.com"); 
      // show return  blahblah.com
      printf("%s",txt);  
      return(0);
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    How about allocating some memory for your domain variable before you try to use it?

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    2
    Quote Originally Posted by rags_to_riches View Post
    How about allocating some memory for your domain variable before you try to use it?
    if I change from
    Code:
    char *domain;
    to
    Code:
    char *domain[BUF_SIZE];

    It has warning messages.

    Code:
    domainsplit_func.c:35: warning: passing arg 1 of `sprintf' from incompatible pointer type
    domainsplit_func.c:36: warning: return from incompatible pointer type
    domainsplit_func.c:36: warning: function returns address of local variable

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    That now becomes an array of pointers to char. How about using malloc?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Recursive function
    By WatchTower in forum C Programming
    Replies: 11
    Last Post: 07-15-2009, 07:42 AM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM