-4

Possible Duplicate:
Is it possible to modify a string of char in C?

#include <stdio.h>

void reverseStr(char *str);

main()
{
  reverseStr("abcdef");
}

void reverseStr(char *str) {
    char *tmp = str;
    char curr;
    while (*tmp != '\0') {
        tmp++;
    }
    tmp--;
    while (tmp > str) {
        curr = *str;
        *str = *tmp;
        *tmp = curr;
        str++;
        tmp--;
    }    
}

When I run it, I get:

/usr/bin/runit/srun_c: line 12:  2809 Segmentation fault      /tmp/run_c_executable

What on earth is going on? I'm practicing for an interview, I'm rusty in my C and wanted to practice something easy but can't for the life of me figure this out. I've noticed the seg fault disappears when I comment out the *str = *tmp; line, and I don't see why that should cause a seg fault. Help appreciated.

Community
  • 1
  • 1
Ken
  • 530
  • 4
  • 11
  • 30
  • 3
    I would not claim good knowledge of C in an interview if I won't be able to find myself why this code is not working. You definitely should compile with warning and debugging info (e.g. `gcc -Wall -g`) and be able to use `gdb` before claiming work knowledge of C. – Basile Starynkevitch Oct 24 '12 at 05:36
  • @BasileStarynkevitch just FYI I am a student, and just learned C last semester. Didn't use it the whole summer. Sorry for the easy question :) – Ken Oct 24 '12 at 06:00
  • 1
    I still suggest to avoid lying in an interview. Say that you have been taught some C but don't know it very well. If the people interviewing you are competent, they will find quite easily that you don't really know C. Your bug is not "strange" as the title claims, it is obvious. If you cannot find by yourself such trivial bugs, you should not claim knowledge of C. Knowing C is at least being able to correct without any help such a simple bug. – Basile Starynkevitch Oct 24 '12 at 06:02
  • @BasileStarynkevitch I never claimed to know it well. The internship position is not one that is C heavy as far as I know - the company prefers to use C as an interview language from what I've read though - needless to say I'm not feeling great about that. I appreciate the advice though and think you're totally right. And I took out the "strange" from the title. – Ken Oct 24 '12 at 06:11
  • I believe that you can't say "I know C" if you are at least not being able to correct that bug alone, without help. I would add that you should not say "I know well C" if you are able to write such bad code at the first place. – Basile Starynkevitch Oct 24 '12 at 06:14
  • @BasileStarynkevitch where are you getting this notion that I've stated "I know C"? The only mention of C on my resume is in the familiar with languages: "X, Y, Z..." I have used C before briefly, and as a student with little experience I think I'm warranted in putting it on my resume. While that bug may seem very trivial to you, you are over 30 years older than me and have tons more experience. As for the bad code part, I don't see what's wrong with this code for interview purposes. Except that I regret not calling "tmp", "end". Anyways, we'll have to agree to disagree on this topic. – Ken Oct 24 '12 at 06:26

3 Answers3

5

You can't modify constant strings. Use a char array instead:

char str[] = "abcdef";

reverseStr(str);
nneonneo
  • 171,345
  • 36
  • 312
  • 383
3

You can't modify string literals — they're stored in readonly memory.

Use:

char str[] = "abcdef";

reverseStr(str);
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
3

Your reversal function looks fine. But it is the way you are calling the function that is causing this crash. You are passing a string literal which are read-only to the function. And modifying a string literal is a undefined behavior, manifesting as crash in your case.

Change

reverseStr("abcdef");

to

char str[] = "abcdef";
reverseStr(str);

where you pass a character array to the function.

codaddict
  • 445,704
  • 82
  • 492
  • 529