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

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.