1

The following code aborts with a segmentation fault error at the commented line. The line is intended to do a simple replacement of one character.

#include <stdio.h>
#include <ctype.h>

int num(char zf[], int n) {
    int i;

    for (i = 0; i < n; i++) {
        // assignment = seg fault
        if (zf[i] == ',') zf[i] = '.';

        if (!isdigit(zf[i]) && zf[i] != '+' && zf[i] != '-' && zf[i] != '.') {
            return 0;
        }
    }

    return 1;
}

int main(void) {
    if (num("-3+3,0", 6)) {
        printf("valid\n");
    } else {
        printf("not valid\n");
    }

    return 0;
}

I'm looking for an explenation why there's an error and what the solution is to this? strncpy? The parameters and datatypes of the function num are not to be changed.

8
  • 7
    Modifying a string literal is undefined behaviour. Here's a question with a similar problem (and there are many more): stackoverflow.com/questions/943312/… Commented Jul 23, 2014 at 19:10
  • 2
    it is undefined behavior to modify a string literal Commented Jul 23, 2014 at 19:10
  • Yep, string literals are constants. Commented Jul 23, 2014 at 19:11
  • Funny, this is a mock exam and "num(char zf[], int n)" is provided by the teacher. Not sure how to solve this at all. Commented Jul 23, 2014 at 19:15
  • 1
    @thpetrus, Don't give it a string literal. Give it something it can actually modify. Commented Jul 23, 2014 at 19:16

3 Answers 3

6

String literals are compiled into the program and stored somewhere in the binary as an array of bytes. You can try the strings command in Linux to find string literals in a binary.

The string constants most likely exist in read only parts of programs and that is why it was made undefined behavior to modify them. A segmentation fault happens when you try to access or modify parts of memory that should not be accessed or modified.

Sign up to request clarification or add additional context in comments.

1 Comment

@TheParamagneticCroissant: I changed the text a little.
1

Though in C string literals have type of non-constant character arrays nevertheless the C Standard does not allow to change them.

4 The same considerations apply to each element of the sequence in a string literal as if it were in an integer character constant

If a program attempts to modify a string literal then its behaviour is undefined.

You should defined main the following way

int main(void) {
    char s[] = "-3+3,0";

    if (num(s, sizeof( s ) - 1 )) {
        printf("valid\n");
    } else {
        printf("not valid\n");
    }
    return 0;
}

Comments

0

Here in if (num("-3+3,0", 6)) , "-3+3,0" is stored in read-only memory. So modifiying it, will result in segmentation fault.

Use an array so that "-3+3,0" is stored in stack. Then we can modify the values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.