Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

basic C question

Status
Not open for further replies.

iggystar

IS-IT--Management
Jul 12, 2001
126
US
I should know this but I dont because I'm so rusty in C it almost hurts so I know somebody out there can answer this in like 2 seconds. I have a char* that I'm sending to a function. I want to do work on that char* in the function and I want the changes I make to stick after I leave the function. So for example:

main()
{
char *fred;

manipulatefred(fred);
}

That's pretty much what I have. What I want to know is, what does the manipulatefred function have to look like so that the changes I make to it actually get fred and not to a copy of fred.

What I'm trying to do in my real program is send an empty string (with memory allocated for it obviously) can get back a full string with real data.

Thanks.
 
definition of the function :

void manipulatefred( char *the_char)


declaration of the function :

void manipulatefred( char *the_char)
{
*the_char = 'A';
}

And your main function :

main()
{
char *fred;

manipulatefred(&fred);
}


That's all.
 
Close but not quite. I actually figured out my problem. it was a bigger program and I came up with a nifty solution but in regards to Spirou's solution I'd like to offer a correction:

#include <stdio.h>
#include <stdlib.h>

void manipulatefred( char **the_char)
{
**the_char = 'A';
}



main()
{
char *fred;
fred = (char *) calloc(sizeof(char),50);

manipulatefred(&fred);
printf(&quot;%s\n&quot;,fred);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top