Modifying a string literal

This is a discussion on Modifying a string literal within the C Programming forums, part of the General Programming Boards category; Hi everyone. I'm trying to learn C99. In this program, which elements are non-conforming? GCC compiles and runs it, but ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    126

    Modifying a string literal

    Hi everyone. I'm trying to learn C99. In this program, which elements are non-conforming? GCC compiles and runs it, but gives me exc_bad_access on the *(p+1).

    Code:
    #include <stdio.h>
    
    int main()
    {
    	char *p = "Hello mother";
    	char  a[] = "Hello father";
    	
    	*(p+1)='u';
    	a[1]='u';
    	
    	void* b = a;
    	b++;
    	
    	printf("%s\n", p);
    	printf("%s\n", a);
    	
    	return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    That's because you're trying to modify a literal string. p is just a pointer to it.

    The second way will work, but you're going to run into buffer overflows if you either try to replace it with a longer string or use strcat() on it...

    It's usually better to allocate string space somewhat generously...
    Code:
    char p[100] = "Hello Mother";
    Now you've got some wiggle room...

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Code:
    	void* b = a;
    	b++;
    As far as I can tell, that shouldn't be valid. Unless I'm mistaken, pointer arithmetic is not allowed on void pointers. Maybe it's a GCC extension that allows it. You could get around it like so, since b points to a char *:
    Code:
    void* b = a;
    ((char *)b)++;
    If you understand what you're doing, you're not learning anything.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    The title of this thread is something that you cannot do, so I'm not surprised you're having trouble.
    Given we can't help you to do the impossible, your solution is to use an actual array, as you have for the variable a.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about string literal
    By pangzhang in forum C Programming
    Replies: 6
    Last Post: 07-31-2010, 08:25 AM
  2. Polymorphism and generic lists
    By Shibby3 in forum C# Programming
    Replies: 9
    Last Post: 07-26-2010, 06:27 AM
  3. Replies: 60
    Last Post: 05-31-2010, 11:57 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

Tags for this Thread


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