User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 374,148 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,483 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 466 | Replies: 4
Reply
Join Date: Sep 2007
Posts: 17
Reputation: BigFormat is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
BigFormat BigFormat is offline Offline
Newbie Poster

Pass a string by reference

  #1  
May 8th, 2008
I have to pass a string to a function that strcats something to it, in K&R I read to pass a pointer to it, but I don't know where to allocate buffer...
Is there a difference betw passing an empty string or not?
My printf starts with some strange symbols, and then the string...

  1. int main() {
  2.  
  3. char *string;
  4.  
  5. //here or in string_concat? or both?
  6. string = (char*) malloc(16 * sizeof(char));
  7.  
  8. string_concat( &string );
  9.  
  10. printf("%s", string);
  11.  
  12. return 0;
  13. }
  14.  
  15.  
  16. void string_concat( char **string ) {
  17.  
  18. //write in string something, need a realloc?
  19. strcat(*string, something);
  20.  
  21. }
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2008
Posts: 274
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Rep Power: 2
Solved Threads: 46
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz in Training

Re: Pass a string by reference

  #2  
May 8th, 2008
>I don't know where to allocate buffer...
Since you're passing the buffer by reference to string_concat, you should allocate the buffer in main, but if string_concat tries to make the string longer than the buffer can hold, you still have a reference to the original pointer and can use realloc.

> Is there a difference betw passing an empty string or not?
The string you pass should always end with a null character because that's what strcat expects for the first argument. You're getting strange symbols because you didn't do that:
string = (char*) malloc(16 * sizeof(char)); 
string[0] = '\0';
string_concat( &string );
Subtlety is the art of saying what you think and getting out of the way before it is understood.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,184
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 34
Solved Threads: 822
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Pass a string by reference

  #3  
May 8th, 2008
Passing by reference only means to pass a pointer, and all arrays are always passed by reference, never by value.

It is not necessary to pass a pointer to a pointer. Passing by reference means to pass a pointer, and String is already a pointer. All that function needs is a pointer to the allocated memory where to do the concantination. Its not necessary to change the value of the original pointer that was allocated in main().
void string_concat( char *string )  {
    
    //write in string something, need a realloc?
    strcat(string, something);    

}
'Politics' is made up of two words, 'poli,' which is Greek for 'many,' and 'tics,' which are blood-sucking insects.
- Gore Vidal
Being ignorant is not so much a shame as being unwilling to learn. - Benjamin Franklin
Reply With Quote  
Join Date: May 2008
Posts: 5
Reputation: InfernalDrake is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
InfernalDrake InfernalDrake is offline Offline
Newbie Poster

Re: Pass a string by reference

  #4  
May 14th, 2008
In practice, i saw always is better to pass the pointer as a self-reference, since the pointer data is volatile (and may reference different memory allocations at different time), so, the only constant is the memory allocation of the pointer, that saved me a lot of debugging hours (and spend a lot by not using it)

for your problem, if timing is not critical, you may check the size of the concatenation, and if size > allocation size, then make a call to realloc (be sure to not abuse from this... realloc allocates a new memory space before deallocating previous one)
Reply With Quote  
Join Date: Sep 2007
Posts: 17
Reputation: BigFormat is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
BigFormat BigFormat is offline Offline
Newbie Poster

Re: Pass a string by reference

  #5  
May 15th, 2008
Ok, I think I've understood the mistery.
Infact I decided to pass a pointer to a pointer because in the interface I'm implementing the function is declared this way, what pros in such a solution (instead of a common char ptr)?

@ InfernalDrake
I'm not sure to have completely got what you mean wits "as a self reference" in this passage:
i saw always is better to pass the pointer as a self-reference, since the pointer data is volatile (and may reference different memory allocations at different time), so, the only constant is the memory allocation of the pointer
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 3:20 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC