g_strdelimit Segmentation fault.

This is a discussion on g_strdelimit Segmentation fault. within the Linux Programming forums, part of the Platform Specific Boards category; Something stupid must be going on. I really can't figure what I am doing wrong Code: #include <stdio.h> #include <glib.h> ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    51

    g_strdelimit Segmentation fault.

    Something stupid must be going on. I really can't figure what I am doing wrong
    Code:
    #include <stdio.h>
    #include <glib.h>
    
    int main(void)
    {
        gchar *t="LeftSide:RightSide";
        g_strdelimit(t, ":", '|');
        printf("%s\n",t);
        return 0;
    }
    According to this :String Utility Functions it should work but the g-strdelimit gives segmentation fault according to gdb.

  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    890
    Quote Originally Posted by mariostg View Post
    Something stupid must be going on. I really can't figure what I am doing wrong
    Code:
        gchar *t="LeftSide:RightSide";
    Above, you defined t as a char pointer. It points to a string literal. String literals are read-only in C.
    Quote Originally Posted by mariostg View Post
    Code:
        g_strdelimit(t, ":", '|');
    Above, the g_strdelimit() function will try to modify the string pointed to by t, but because it is read-only, you'll get a segmentation violation.

    What you intended, obviously, was to declare a string variable, initialized to your string, right? That would be
    Code:
        gchar t[] = "LeftSide:RightSide";
    The difference between this one and the one you used that here, the string literal only defines the initial value of the char array t, whereas in your code, you defined t to point to the string literal itself.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    51
    I was just about to post that I solved it but you replied before I could. I tend to forget about this important detail.
    Thanks anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a segmentation fault
    By rohan_ak1 in forum C Programming
    Replies: 8
    Last Post: 05-06-2008, 05:36 PM
  2. Segmentation fault
    By (Slith++) in forum C++ Programming
    Replies: 4
    Last Post: 05-06-2007, 04:47 AM
  3. segmentation fault??
    By snappleapple in forum C Programming
    Replies: 9
    Last Post: 04-28-2007, 12:56 AM
  4. Segmentation Fault
    By warfang in forum C++ Programming
    Replies: 9
    Last Post: 04-23-2007, 02:42 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

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