开发者

Change string pointed by pointer

开发者 https://www.devze.com 2022-12-09 22:48 出处:网络
Many funct开发者_运维问答ions in c take pointer to constant strings/chars as parameters eg void foo(const char *ptr) .However I wish to change the string pointed by it (ptr).how to do it in cYou can j

Many funct开发者_运维问答ions in c take pointer to constant strings/chars as parameters eg void foo(const char *ptr) . However I wish to change the string pointed by it (ptr).how to do it in c


You can just cast away the const:

void evil_function(const char *ptr)
{
  char *modifiable = (char *) ptr;

  *modifiable = 'f';
}

Note that this is dangerous, consider cases where the data being passed to the function really can't be changed. For instance, evil_function("bar"); might crash since the string literal is often placed in read-only memory.


Don't do it as it will cause your code to behave unpredictably. Basically the string pointed by const char* may be stored in the read-only section of your program's data and if you try to write something there, bad things will happen. Remember that foo can be called as foo("Test"), here you have not allocated memory for "Test" yourself, you just have a pointer to memory which contains the string. This memory may be read-only.


You can copy it to another piece of memory, and modify it there.

If you cast it to non-const, and then modify, chances are good you'll just segfault.


void foo(const char *x);
char data[4] = "Hi!";
int sum = 0;
for (int k=0; k<strlen(data); k++) {
    foo(data);           /* foo first */
    sum += data[k];
}
printf("%d\n", sum);

Because foo() does not change its argument, the compiler can change this code to

void foo(const char *x);
char data[4] = "Hi!";
int sum = 0;
for (int k=0; k<strlen(data); k++) {
    sum += data[k];      /* sum first */
    foo(data);
}
printf("%d\n", sum);

But, if foo() changes the values in the data array, the results will be different according to the order the compiler chose to code the loop!

In short: don't lie to your compiler

How to lie to the compiler

Cast the const away

void foo(const char *readonly) {
    char *writable = (char *)readonly;
    /* now lie to the compiler all you like */
}


by notation "const char *ptr" we are telling Compiler that ptr contains should not be changed.

Just, we can't change it!


The whole reason the const is so to express that the underlying content is not to be modified by this function, so don't change it because that will most likely break some code which is relying on the constness. other then that you can always cast the constness away using either const_cast<char*> or by directly casting the pointer


If you do it like this:

void dont_do_this_at_home(const char *ptr)
{
  char **steve-o_ptr = (char **) &ptr;
  char *bam_margera = "viva la bam";

  *steve-o_ptr = bam_margera;
}

Then the pointer that you send into the function will be changed despite being a const pointer, the other suggestions so far only let you change the contents of the string, not the pointer to the string.

And I agree with the others that you shouldn't, ever, "un-const" any parameter you get, since the callee may really depend on that there are no side-effects to the function regarding those parameters.

There is also this way to get rid of the warnings/errors

typedef struct {
    union {
        const void* the_const;
        void* the_no_const;
    } unconsting;
}unconst_t;

/* Here be dragons */
void* unconst_pointer(const void* ptr) {
    unconst_t unconst.unconsting.the_const = ptr;
    return unconst.unconsting.the_no_const;
}

As you see it is quite possible and popular to actually do this, but you have to know what you are doing or mysterious faults may appear.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号