0

For the below question,

Write a function replace which takes a pointer to a string as a parameter, which replaces all spaces in that string by minus signs, and delivers the number of spaces it replaced.

Thus

   char *cat = "The cat sat";
   n = replace( cat );

should set

    cat to "The-cat-sat"

and

   n to 2.

In the above problem, char *cat="That cat sat" is nothing but const char *cat="That cat sat"

Here is my non-working code for this problem,

#include<stdio.h>
int replace(char[]);
int main(void){
  const char *cat = "The cat sat";
  int n = replace((char[])cat);
  printf("\n Now the output is: \"%s\"", cat);
  printf("n is %d", n);
}

int replace(char cat[]){
  int count =0;
  for(int i =0; cat[i] != '\0'; i++){
    if(cat[i] == ' ') {
      cat[i] = '-';
      count++;
    }
  }
  return count;
}

To analyse this code,

const char *cat is pointing to a buffer(The cat sat) that cannot be modified.

So, am casting((char[])) to make cat a (non-const)modifiable buffer, before calling replace((char[])cat). Because, C is loosely typed

But the program still segments at cat[i]='-'

How do I understand this problem?

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • "make `cat` a (non-const)modifiable buffer," by casting and then calling `replace()` invokes undefined behavior - it might work, it might not, . Better to make a copy of the data pointed by `cat` in a modifiable buffer and call `replace()` on that. – chux - Reinstate Monica Oct 13 '16 at 22:45
  • @chux Might work? I want to take a call on `(char[])cat` – overexchange Oct 13 '16 at 22:46
  • 4
    The exercise you were given is wrong: There is no way to change a string literal, so the exercise is impossible. They should change it to `char cat[] = "The cat sat";` to make the exercise correct. – interjay Oct 13 '16 at 22:56
  • @interjay Confirmed from you. You helped me – overexchange Oct 13 '16 at 22:57
  • `char *` is not the same as `const char *` as you say in the question – M.M Oct 13 '16 at 23:05
  • Technically it is possible to meet the stipulation, with some macro trickery, but I doubt this is what was meant by the exercise – M.M Oct 13 '16 at 23:06

2 Answers2

1

I cover this question at great length in this question:

Difference between declared string and allocated string.

In short, the manner in which you declare the string, regardless of the const storage class modifier, is stored in RODATA (ie: text segment), so re-casting it to non-const (something you should try to avoid, since there are few legitimate reasons for doing this) doesn't help you.

In , if you want to make a buffer selectively-writable, hide it away in a context variable or as a static variable within a source file, and only allow access to it via private API calls (ie: "getter"/"setter" functions).

Cloud
  • 18,753
  • 15
  • 79
  • 153
1

Strings inside "" are called string literals. These are immutable. So, instead of

 const char *cat = "The cat sat";

You can write:

char cat[20]; // This or calloc/malloc. Your choice
strcpy(cat, "The cat sat"); 

And then pass this in to the function.

  • 1
    For safety reasons use strncpy (strcpy with max copy length), to avoid errors with missing „\0“ stings – Kraego Aug 07 '21 at 07:57