Hello Friends,
Suppose I have a function like -

void myFunc( char* str)
{
     .......
     .......
}

And I have to pass a pointer of type "const char*" into that function.
Now my question is that can I pass the pointer like following way (ie by custing it into a "char*"),

const char* ptr = "ABCD";
.....
.....
void myFunc( [B](char*) ptr[/B])
.......
.......

Thanx,
Amit

Dani AI

Generated

Casting a const pointer to char * will compile (with an explicit cast), but it doesn't make the data writable. If the target points at a string literal or an object originally declared const, any write through the casted char * is undefined behavior and may crash on some platforms. That is the core risk behind the "it works on my machine" experiments ().

Two practical, safe options (echoing ): either make the API const-correct, or call the API with a writable copy. If changing the API is impossible, create a modifiable buffer, copy the contents, call the legacy function, then free or copy results back as needed. Example pattern:

const char *src = /* source string */;
size_t n = strlen(src) + 1;
char *tmp = malloc(n);
if (tmp) {
    memcpy(tmp, src, n);
    legacy_api(tmp);   /* legacy_api expects a char* */
    free(tmp);
}

If the original data is actually owned and writable (for example, allocated earlier with malloc), a cast may work in practice, but it is fragile and hides the real mismatch. Stronger safety checks: compile with warnings that catch qualifier casts (e.g. -Wcast-qual, -Wwrite-strings, -Wall -Wextra), run under AddressSanitizer (-fsanitize=address) or Valgrind, and review the function body for write operations (strcat/strcpy/memcpy to the parameter).

Summary: prefer const-correct signatures where possible; if not, pass a writable copy. Experimentation helps, but pair it with static checks and runtime sanitizers before relying on a casted pointer in production.

Recommended Answers

All 4 Replies

Instead of giving a book answer, I figured I'd help you help yourself: did you try it? You can learn a lot just from experimenting and seeing if something works or not.

Hi,
Infarction, thanx for ur reply.
I've already tried this and it is working fine.
But my question is that whether it will cause any memory related problem or not. I can't change the signature of the both of the function and the pointer. Can u pls tell me that whether it has any other impact or not. Or can u pls suggest me any other alternative to do the same.

Amit

Well that really depends on whether myFunc was declared with char* because
- the programmer was too lazy to say const
- because the function does actually modify the string.

The first is fixed by fixing the code.
The second is fixed by making a modifiable copy of the string constant, then passing that to myFunc.
Neither involves a cast.

> You can learn a lot just from experimenting and seeing if something works or not.
Which is where all the "works for me" posts come from. Noobs who try something, observe an effect and then think that applies to everyone else as well.

One very different effect this particular poster is likely to notice is the effect of trying to modify a string constant.

Thanx a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.