Like Tree1Likes
  • 1 Post By QuantumPete

What's wrong with this code?

This is a discussion on What's wrong with this code? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <string.h> void swap_chars(char* str, int i, int j); int main() { char* s = "Hello World ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    5

    What's wrong with this code?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void swap_chars(char* str, int i, int j);
    
    int main()
    {
        char* s = "Hello World of C";
        
        printf("Before: %s\n", s);
    
        swap_chars(s, 0, 1);
        printf("After: %s\n", s);
        
        getchar();
    }
    
    void swap_chars(char* str, int i, int j)
    {
        char t = str[i];
        
        str[i] = str[j];
        printf("here");
        str[j] = t;
    }

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    It's not particularly helpful to just ask "What's wrong with this code?" - give us some help and tell us what the problem is that you're seeing.
    In this case, you're trying to change a string literal, which sits in a read-only part of memory. So trying to write to it on line 22 will cause your program to crash.
    MK27 likes this.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    5
    Quote Originally Posted by QuantumPete View Post
    It's not particularly helpful to just ask "What's wrong with this code?" - give us some help and tell us what the problem is that you're seeing.
    In this case, you're trying to change a string literal, which sits in a read-only part of memory. So trying to write to it on line 22 will cause your program to crash.
    Sorry maybe I should rephrase the title. I agree with you that the problem is that I am trying to change a read-only variable.

    Any suggestions on how I can fix it? I just want to be able to swap characters in strings.

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    You need to create a char array and then fill it with the string literal:

    Code:
    char s[] = "Hello World of C";
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    5
    You are right. I was trying to change the read-only string in line 8.
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is wrong with my code?
    By miller1737 in forum C++ Programming
    Replies: 2
    Last Post: 01-22-2010, 05:34 PM
  2. something wrong with code
    By dankassdann in forum C++ Programming
    Replies: 5
    Last Post: 10-27-2006, 12:27 PM
  3. What's wrong with this code?
    By chrischar in forum C Programming
    Replies: 2
    Last Post: 06-17-2003, 04:08 AM
  4. What is wrong with my code?
    By kewee7197 in forum C++ Programming
    Replies: 2
    Last Post: 10-11-2002, 07:40 AM
  5. what is wrong with this code?
    By Jennifer in forum Windows Programming
    Replies: 1
    Last Post: 01-06-2002, 08:50 AM

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