Thread: Whats wrong in this code?

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    3

    Whats wrong in this code?

    char *strlwr(char *string);
    char *a="VIJAY";
    char *c;
    c=strlwr(a);
    printf("%s",c)

    I am getting memory violation error at step 4.

    Thanks,
    Vijay

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Vijay_K
    Whats wrong in this code?
    1. You did not use code tags
    2. You did not provide a complete program
    3. You did not show the implementation of "strlwr()"

    There could be several reasons for this error, but we can't say for sure without seeing how the function is implemented.
    Last edited by Matticus; 02-03-2016 at 06:39 AM.

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    3
    Hi Matticus,

    Dont know what you are asking, I am newbie in C, please help. I just want to know what is wrong in step 3;

    Here is the code:

    Code:
    Action()
    {
        
        char *a="SAM";
        char *c;
        c=strlwr(a);
        lr_output_message("%s",ptr_one);
        
        
        return 0;
    }
    Error:

    Action.c(9): Error: C interpreter run time error: Action.c (9): Error -- memory violation : Exception ACCESS_VIOLATION received.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    You were already asked, but I'll ask again. What is strlwr? Where did you find it, and what does it do? Do you have the source code for it, or is it a library function somewhere?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    "strlwr()" is not a standard C library function, but apparently some implementations modify the string it receives.

    Try declaring "a" as an array:

    char a[]="SAM";

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Almost certainly, strlwr() will try to do something with the string in place, rather than making a copy of it first.

    So you need to know that something like
    char *s = "hello";
    stores "hello" in read-only memory.

    If you try to modify it, you get an access violation.

    Compare this with
    char s[] = "hello";
    which allocates some memory (called s), and populates that with the characters "hello".

    Now this is something you should be able to pass to strlwr().
    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.

  7. #7
    Registered User
    Join Date
    Feb 2016
    Posts
    3
    Hi,

    Here is the link you will understand. http://lrhelp.saas.hp.com/en/latest/....htm#kanchor87
    I want to save it some variable that is returned through the function strlwr.
    But I am not able to save it in a variable why is this?

    Here you can see the example as well, they are using strdup, I dont know why they are using it, big confusion
    Last edited by Vijay_K; 02-03-2016 at 10:39 AM.

  8. #8
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Vijay_K View Post
    Hi,

    Here is the link you will understand. http://lrhelp.saas.hp.com/en/latest/....htm#kanchor87
    I want to save it some variable that is returned through the function strlwr.
    But I am not able to save it in a variable why is this?

    Here you can see the example as well, they are using strdup, I dont know why they are using it, big confusion
    Matticus and Salem have answered your original question.
    You can't pass a pointer to a string literal to strlwr since string literals are often stored in memory you can't write to.
    Instead you must create your own array for the string data and pass that.

    The reason they use strdup in that example is because strlwr modifies the input string and they don't want to modify the original string. So they make a copy of it with strdup, call strlwr, do whatever needs to be done with it, and then they free the copy.
    Last edited by algorism; 02-03-2016 at 10:55 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Whats wrong in my code?
    By Cyberman86 in forum C Programming
    Replies: 5
    Last Post: 04-10-2009, 08:25 AM
  2. whats wrong with this code?
    By jcafaro10 in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2007, 01:23 PM
  3. Whats wrong with this code?!
    By Death_Wraith in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2004, 04:18 PM
  4. whats wrong with my code?
    By moemen ahmed in forum C++ Programming
    Replies: 3
    Last Post: 02-24-2002, 07:43 PM
  5. whats wrong with my code?
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 11-01-2001, 04:18 PM