-1
#include <stdio.h>
#include "../library/string.c"

int test(char* left, char* right) {
    while ( *left != '\0') {
        printf("%p\t%d, %d\n", left, (*left) + 1, *left);
        left++;
    }
    return 0;
}

int main() {
    char* a = "hello,kittz";
    char* b = "hello,kitty";
    test(a, b);
    return 0;
}

when I execute the codes above , here are the output:

0x40073f    105, 104
0x400740    102, 101
0x400741    109, 108
0x400742    109, 108
0x400743    112, 111
0x400744    45, 44
0x400745    108, 107
0x400746    106, 105
0x400747    117, 116
0x400748    117, 116
0x400749    123, 122

But when i change the code to be :

int test(char* left, char* right) {
    while ( *left != '\0') {
        printf("%p\t%d, %d\n", left, (*left)++, *left);
        left++;
    }
    return 0;
}

The output tell me that there is something wrong occurs. Here is the output: Segmentation Fault (core dumped).

Saint
  • 1,492
  • 1
  • 11
  • 20
  • 3
    Please format your code before posting it; you'd have greater chances of getting an answer. – legends2k May 26 '16 at 15:51
  • 1
    Have you tried compiling this with debug flags (using `-g`), dropping this into `gdb` and stepping through your code to pinpoint where the segfault is happening? – Jules May 26 '16 at 15:52
  • 2
    i assume those `**` are meant to be a highlight not actual code. Can you take them out and show the actual code you are running – pm100 May 26 '16 at 15:53
  • 1
    String literal can not be changed. – BLUEPIXY May 26 '16 at 15:54
  • 1
    Use a debugger to see where the strings hello,kitt* are stored. – sjsam May 26 '16 at 16:06

2 Answers2

4
(*left)++

is trying to change the value that left points at - ie it is trying to change the compile time literal 'k' to 'l'. This is not allowed (strictly speaking its 'Undefined Behavior' - this means it might work on some systems sometimes, or it might format your hard drive, or light up the xmas tree on the white house lawn...)

pm100
  • 48,078
  • 23
  • 82
  • 145
  • -1 -- that is actually quite fine thing to do in C on general-purpose computers in reasonable compiler. You may have problem if string literals are placed in read-only memory – lowtech May 26 '16 at 15:58
  • 1
    Upvoted. A correct answer. @lowtech Modifying a string literal is undefined behavior in C. – 2501 May 26 '16 at 16:02
  • if you want strict talk specify what C you are talking about. – lowtech May 26 '16 at 16:04
  • @lowtech ISO 9899 See here: https://stackoverflow.com/tags/c/info, this is the tag being used on this question. – 2501 May 26 '16 at 16:08
  • Correct answer, but we also to add that this is an undefined behaviour only in case of global variable, in case of local variable it's accepted isn't ? – fedi May 26 '16 at 16:09
  • 2
    @lowtech. ideone blows up, the OP's compiler blows up. Maybe on your machine ti works. But its clearly undefined behavior – pm100 May 26 '16 at 16:10
  • @fedi String literals have only static storage duration. Modifying a string literal is undefined behavior. – 2501 May 26 '16 at 16:11
  • what version of C language you are talking about? – lowtech May 26 '16 at 16:12
  • 1
    @lowtech - its UB, you are demostrating that by asking what compiler it is. If it was defined behavior that would be a redundant question. Different modern compilers do different things - proof – pm100 May 26 '16 at 16:14
  • UB for this example is not part of language definition - that is my point. it is only design choice of compiler writer - possibly easily overriden compiler switches. In any case UB is not a reason why seg fault occurs – lowtech May 26 '16 at 16:19
  • 1
    @2501, OK, you're right, No Modification for string Literal – fedi May 26 '16 at 16:20
  • If the string was defined like, char a[12] = {'h','e','l','l','o',',','k','i','t','t','z'}; it will works, but i'am not sure to undrestand why it works – fedi May 26 '16 at 16:41
  • @fedi That is not a string literal, just as: `char a[]= "hello"` isn't one. – 2501 May 26 '16 at 16:48
0

For finding a segfault, you can use many tools.

First compile with the flags gcc -Wall -Wextra -Werror could sometime help.

Also, you could use as @Jules says to add the -g flag for helping using gdb. and set env MallocStackLogging 1 in your gdb environment is REALLY helpful ! and can show you the line where your segfault occurs

For navigate in a string you can use an Int like :

int i = 0;
char str[] = "hello world";

while (str[i])
    putchar(str[i++]);

But you can also use pointer like

while (*str)
    *str++;
albttx
  • 3,444
  • 4
  • 23
  • 42
  • thanks for your suggestions, which is so helpful for me since I am freshman to learn C program recently. – Saint May 26 '16 at 16:01