-6

I have this function that changes the string to all lower cases.

I am trying to create a unit test for this function, but I think I am passing the argument wrong, and I get this error.

Segmentation Fault (core dumped)

This is my code.

void example (char const * str1, int length, char * str2) {
    int i;
    for(i = 0; i < length; i++) {
        *(str2 + i) = putchar(tolower( *(str1 + i) ));
    }
}

void testexample() {
    char * str1 = "TEST";
    char * str2 = "";
    example( str1, 4, str2);
    printf("%s\n", *str2);
}

int main() {
    testexample();
    return 0;
}

str1 is the original string, and n is the length of the string, and str2 is the all lower case version of str1.

I've been stuck trying to solve this problem for a while now.

I appreciate any help. Thank you.

neko_studies_prog
  • 133
  • 1
  • 1
  • 5
  • 2
    How did this even compile? – EOF Feb 21 '17 at 17:09
  • `str2` points to a string literal, which is likely read-only... and it's not even long enough to copy `str1` into anyway. – Dmitri Feb 21 '17 at 17:16
  • 1
    C11 draft standard n1570: *6.5.16 Assignment operators Constraints 2 An assignment operator shall have a modifiable lvalue as its left operand.*, *6.3.2.1 Lvalues, arrays, and function designators [...]A modifiable lvalue is an lvalue that does not have array type, does not have an incomplete type, does not have a const- qualified type,[...]*. Your compiler was obligated to issue a diagnostic message. – EOF Feb 21 '17 at 17:16
  • From the issues with your code it's clear that you don't know some fundamentals about the c programming language, I suggest reading about it and then try this again. For instance, do you know that `putchar()` does? Do you understand that string literals are read only? Do you understand that arrays do not grow automatically in c? Do you know that strings in c are arrays? – Iharob Al Asimi Feb 21 '17 at 17:22
  • I'm sorry I mistyped. Str2 is not const. I updated it. – neko_studies_prog Feb 21 '17 at 17:23
  • @neko_studies_prog That means nothing, `str2` is still read only because it points to a string literal. The `const ` would merely prevent it from compiling. And also, if you make it an array and initialize it with the literal `""` then there is no enough room, you can try `char str2[5]`. – Iharob Al Asimi Feb 21 '17 at 17:23
  • 1
    @IharobAlAsimi: It does mean that *this* remaining error is not automatically caught by the compiler. – EOF Feb 21 '17 at 17:26
  • @EOF I stand corrected, it's not better and it's not the same, it's worst. – Iharob Al Asimi Feb 21 '17 at 17:29

2 Answers2

0

In your main:

char * str2 = "";

and then you pass str2 as a parameter, but it has too little storage and that storage is further read-only memory (points to a literal). The result is your crash. Use:

char str2[32] = 0;

or any length you need, including a null terminating character.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
0

also you write printf("%s", *str2). if str2 is a string so *str2 is a char.

Roy Avidan
  • 769
  • 8
  • 25