Thread: can't copy value back into pointer

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    42

    Question can't copy value back into pointer

    Hi,

    I want to implement a version of strlwr() but can't seem to be copying the lower case value back into the string.
    My test program with the function looks like:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    int strlwr(char* s)
    {
        char* tmp = s;
        char r;
    
        while (*tmp) {
            printf("tmp %c\n",*tmp);
            r = tolower(*tmp);
            printf("r %c\n",r);
            *tmp = r;
            tmp++;
        }
    
        return 0;
    }
    
    int main (void)
    {
      char *addr = "AB:CD:EF:00:12:34";
      printf("addr: %s\n", addr);
      strlwr(addr);
      printf("lower: %s\n", addr);
    
    return 0;
    }
    and I get a seg fault at *tmp = r; but I don't undersatand why, any help would be greatly appreciated!

    Thanks!

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    char *addr = "AB:CD:EF:00:12:34";
    You cannot modify a string literal (which is what "addr" points to).

    Either make "addr" an array, or allocate memory, so you can work with a modifiable string.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    42
    Quote Originally Posted by Matticus View Post
    Code:
    char *addr = "AB:CD:EF:00:12:34";
    You cannot modify a string literal (which is what "addr" points to).

    Either make "addr" an array, or allocate memory, so you can work with a modifiable string.

    Oh yes, that's what it was! Thank you!

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    42
    What's the advantage (if any) from using a temporary pointer (tmp) rather than working with the s directly?

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    In this case, there are no advantages to using "tmp", since any modifications made to the value of pointer "s" are local to the function and not seen by the caller.

    Code:
    #include <stdio.h>
    
    void print_string(const char *s);
    
    int main(void)
    {
        const char *s = "Test";
    
        puts(s);
        print_string(s);
        puts(s);
    
        return 0;
    }
    
    void print_string(const char *s)
    {
        while( *s )
            putchar(*(s++));
        putchar('\n');
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy array into pointer
    By a.mlw.walker in forum C++ Programming
    Replies: 7
    Last Post: 06-20-2013, 01:35 PM
  2. copy string by pointer
    By mohsen in forum C Programming
    Replies: 6
    Last Post: 01-25-2012, 11:59 AM
  3. Copy pointer address to buffer and convert it back
    By vincent_rigouts in forum C Programming
    Replies: 6
    Last Post: 05-26-2011, 01:21 PM
  4. Problem with passing back pointer pointed to string
    By whichet in forum C Programming
    Replies: 9
    Last Post: 11-21-2007, 07:55 AM
  5. I'm back! need help with a simple copy program
    By Rune Hunter in forum C++ Programming
    Replies: 11
    Last Post: 10-16-2004, 07:52 PM

Tags for this Thread