Thread: g_strdelimit Segmentation fault.

  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
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    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, 04:36 PM
  2. Segmentation fault
    By (Slith++) in forum C++ Programming
    Replies: 4
    Last Post: 05-06-2007, 03:47 AM
  3. segmentation fault??
    By snappleapple in forum C Programming
    Replies: 9
    Last Post: 04-27-2007, 11:56 PM
  4. Segmentation Fault
    By warfang in forum C++ Programming
    Replies: 9
    Last Post: 04-23-2007, 01:42 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM