char **ppstring will probably work better than ppstring ** ;)
Main Topics
Browse All TopicsHi,
I'd like to know how to pass a string to a function by reference.
e.g.
main()
{
char * string;
myfunction(string);
}
myfunction(string)
{
//modify string and send back to main
}
I would like myfunction to be able to malloc 'string' and modify the contents of it so as it can be used in the main() function.
Any ideas?
Thanks
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Ooops, copy and paste typo, well spotted and thanks :)
Again, with the correction....
=-=-=-=-=-=-=-=-=-=-=-=-=-
C doesn't support references only pointers and this is what a char * already is. If you want to be able to modify the pointer itself then you need to pass a pointer to a pointer (char **).
myfunction(char ** ppstring)
{
//modify string and send back to main
* ppstring = <new value>;
=-=-=-=-=-=-=-=-=-=-=-=-=-
}
main()
{
char * string;
myfunction(&string);
}
@mnh,
Generally speaking on EE if you are going to post an answer that duplicates someone elses you don't... or you at least mention the original post and explain why you feel your post adds additional value to the question. Unlike other forums, experts are rewarded for their efforts here on EE and duplicate posts both dilute the value of the thread and undermine the answers already provided.
By all means, if you have something additional to add value to a thread then do so; however, just posting code that duplicates a answer benefits no one.
evilrix
>> My post's added value is an example about how to allocate and free memory
That was never part of the question so it adds no value. The asker wanted to know how to pass the string by reference in C so it could be modified.
>> Part of this was explicitly asked by the question's author.
No, it was stated as a requirement that needed to be fulfilled but the question was how to pass the string by reference to allow it to be assign a value or modified.
mnh, I can see you are new here so please accept these words as guidance to help you avoid getting involved in confrontation early on in your EE expert career.
At this point there is no need to reply to me and pollute this thread with a stream of off-topic posts. If you'd like to discuss this further I'd be happy to open a private discussion (e-mail me, my address is in my profile).
Thanks.
-Rx.
Business Accounts
Answer for Membership
by: evilrixPosted on 2009-10-07 at 08:45:37ID: 25516743
C doesn't support references only pointers and this is what a char * already is. If you want to be able to modify the pointer itself then you need to pass a pointer to a pointer (char **).
myfunction(ppstring **)
{
//modify string and send back to main
* ppstring = <new value>;
}
main()
{
char * string;
myfunction(&string);
}