Thread: Why does this code segfault?

  1. #1
    Registered User
    Join Date
    Jun 2018
    Posts
    7

    Why does this code segfault?

    I'm trying to make a program that separates two values in a string
    Code:
    #include <stdio.h>
    
    void separarUrlYPalabra(char *buff, char *url, char *palabra) {
        int esUrl = 1;
        int contador = 0;
        int largoUrl = 0;
        while(1) {
            printf("%d\n", contador);
            if(*(url+contador) != '{') {
                if(esUrl) {
                    *(url+contador) = *(buff+contador);
                    contador++;
                }
                else {
                    if(*(buff+contador) == '0')
                        return;
                    *(palabra+contador-largoUrl) = *(buff+contador);
                    contador++;
                }
            }
            else {
                if(*(buff+contador+1) == '}'){
                    largoUrl = contador + 2;
                    contador = contador + 2;
                    esUrl = 0;
                }
                else {
                    *(url+contador) = *(buff+contador);
                    contador++;
                }
            }
        }
    }
    
    int main() {
    //delimiter {}
    //0 is to know the end of the word
        char buffer[1000] = "https://cboard.cprogramming.com/newthread.php?do=postthread&f=4{}word0";
        char palabra[21];
        char url[90];
        separarUrlYPalabra(buffer, url, palabra);
        printf("%s\n%s\n", url, palabra);
    }

  2. #2
    Registered User john.c's Avatar
    Join Date
    Dec 2017
    Posts
    356
    Code:
    void separate(const char *line, char *url, char *word) {
        while (*line) {
            if (*line == '{' && *(line+1) == '}') {
                line += 2;
                while (*line && *line != '0')
                    *word++ = *line++;
                *word = '\0';
                break;
            }
            *url++ = *line++;
        }
        *url = '\0';
    }
    Simplicity is the ultimate sophistication.

  3. #3
    Registered User
    Join Date
    Jun 2018
    Posts
    7
    man that is a well polished code. I have a question, when will line become NULL so while(*line) is false?

  4. #4
    Registered User john.c's Avatar
    Join Date
    Dec 2017
    Posts
    356
    Quote Originally Posted by qqsszz View Post
    when will line become NULL so while(*line) is false?
    line won't become NULL, but it will eventually point to the '\0' character at the end of the string, so *line will be 0 (i.e., false).
    Simplicity is the ultimate sophistication.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault
    By Clayton Cuteri in forum C Programming
    Replies: 6
    Last Post: 02-20-2013, 11:41 PM
  2. Segfault... Why?
    By jonheese in forum C Programming
    Replies: 14
    Last Post: 11-21-2011, 06:02 PM
  3. Sometimes segfault, sometimes not
    By jcafaro10 in forum C Programming
    Replies: 18
    Last Post: 04-07-2009, 06:53 PM
  4. Segfault
    By oddball in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 07:53 AM
  5. segfault with gcc, but not with TC
    By koodoo in forum C Programming
    Replies: 15
    Last Post: 04-23-2007, 09:08 AM

Tags for this Thread