Thread: Is there a simple way or condition to test for a string literal (non mutuable string)

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    93

    Exclamation Is there a simple way or condition to test for a string literal (non mutuable string)

    I have a program that extracts data records and strings from a data file and some that are in the actual program (mutable and non mutable). I have a function that will work on the incoming string and clean it up (removing whitespace etc).

    int cleanup(char * a).


    Now I want to do this because I've been getting seg faults within the function for acting on a non mutable strings.

    Is there a condition I can place either before the call or within the function in order to let the function to quit or resume duty?

    Thanks in advance.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I've been getting seg faults within the function for acting on a non mutable strings.
    That's a quality of implementation issue on the part of the caller. If you're the caller, fix your code. If you're not the caller, there's really nothing you can do to save other people from their stupidity. Put simply, there's no standard way to test for a string literal when given a pointer to char.
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > int cleanup(char * a).
    The best answer is to have
    int cleanup ( char *cleanString, const char *dirtyString );

    Then it really doesn't matter whether the string is const or not, and you don't change your source string in unexpected ways (this is one of the health warnings on the use of strtok() for example).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's a way you can usually tell. Try modifying it. If your program crashes, it was likely a string literal.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    The other way to do it is to parse an incoming string without any modification.
    For example, you've got a string " this is some data ". Your task is to trim the spaces.
    Consider this data structure to hold the result:
    Code:
    struct vec {
        const char *ptr;
        int  len;
    };
    Having that, you do this:
    Code:
    const char *data = "   this is some data     ";
    struct vec  v;
    
    cleanup(data, &v);
    printf("Trimmed string: [%.*s]\n", v.len, v.ptr);

  6. #6
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    Yeah, the function cleanup(). It may be implemented as follows:
    Code:
    void cleanup(const char *str, struct vec *v)
    {
        /* Trim left spaces */
        for (v.ptr = str; *str != '\0' && *str != ' '; str++)
            v.ptr = str;
    
        /* Trim right spaces */
        v.len = strlen(v.ptr);
        while (v.len > 0 && v.ptr[v.len] == ' ')
            v.len--;
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void cleanup(const char *str, struct vec *v)
    {
        /* Trim left spaces */
        for (v.ptr = str;
    Notice anything odd here?


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    yeah, sure, a typo.
    -> should be used instead of a dot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  4. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM