1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>



int toggleChars (char* string, char* letters){
    int count = 0;
    char* scurrent = string;
    while(*scurrent != '\0'){
        char* lcurrent = letters;
        while(*lcurrent != '\0'){
            if(*scurrent == *lcurrent){
                *scurrent = '0';
                count += 1;
            }
            lcurrent += 1;
        }
        scurrent += 1;
    }
    return count;
}


int main(){

    char* str = malloc(50);
    char* letters = malloc(20);

    str = "Hi how R U today?";
    letters = "HhiR";

    int x = toggleChars(str, letters);

    printf("str: %s\ncount: %d", str, x);


    return 0;
}

This is just a testing function I made to study for an upcoming exam in C programming. When I compile this and run it, it gives me a segfault. I have deduced with some testing that it is caused by the line

*scurrent = '0';

So it means that I should not change the character at that memory location. But then if I do want to change it, what would I have to do?

  • 1
    You can't assign a string that way. In `str = "Hi how R U today?";` you are changing the pointer `str` from the memory got from `malloc` to the constant string `"Hi how R U today?"` which is in read only memory. Trying to change the string will give a memory violation. Use instead `strcpy(str, "Hi how R U today?");` – Frankie_C Oct 27 '19 at 09:05
  • Possible duplicate of [Allocate memory and save string in c](https://stackoverflow.com/questions/8600181/allocate-memory-and-save-string-in-c) – mkrieger1 Oct 27 '19 at 09:06
  • @Frankie_C Thanks! It worked. I have another question. If I just declared char* str = "Hi how R U today" and same thing with letters without, it wouldn't work either right? Do I have to call malloc and copy the strings into the memory? Is there a better way to this? – IUissopretty Oct 27 '19 at 09:09
  • You can create a local char array and **initialize it** with the string you need: `char str[50] = "Hi how R U today?";`. The compiler CRT will provide for copyng the string. – Frankie_C Oct 27 '19 at 09:38

1 Answers1

0

There is a problem with your code, the line str = "Hi how R U today?"; will not copy the string to the character array you dynamically allocated, instead it will point to the const char* (read only copy).

So because of this when you are trying to change the contents of str through the toggleChars() function it is throwing a segmentation fault.

You can edit your code as follows:

#include <stdio.h>

int toggleChars (char* string, char* letters){
    int count = 0;
    char* scurrent = string;
    while(*scurrent != '\0'){
        char* lcurrent = letters;
        while(*lcurrent != '\0'){
            if(*scurrent == *lcurrent){
                *scurrent = '0';
                count += 1;
            }
            lcurrent += 1;
        }
        scurrent += 1;
    }
    return count;
}

int main(){
    char str[] = "Hi how R U today?";
    char letters[] = "HhiR";
    int x = toggleChars(str, letters);
    printf("str: %s\ncount: %d", str, x);
    return 0;
 }

Instead of this, if you want to use dynamic memory allocation you can use strcpy() function from standard library. https://www.geeksforgeeks.org/strcpy-in-c-cpp/

Thanks, hope this helps,

-Rajkumar