•
•
•
•
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
![]() |
•
•
Join Date: Sep 2007
Posts: 17
Reputation:
Rep Power: 1
Solved Threads: 0
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...
Is there a difference betw passing an empty string or not?
My printf starts with some strange symbols, and then the string...
c Syntax (Toggle Plain Text)
int main() { char *string; //here or in string_concat? or both? string = (char*) malloc(16 * sizeof(char)); string_concat( &string ); printf("%s", string); return 0; } void string_concat( char **string ) { //write in string something, need a realloc? strcat(*string, something); }
>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:
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.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,184
Reputation:
Rep Power: 34
Solved Threads: 822
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().
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
- Gore Vidal
Being ignorant is not so much a shame as being unwilling to learn. - Benjamin Franklin
•
•
Join Date: May 2008
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
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)
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)
•
•
Join Date: Sep 2007
Posts: 17
Reputation:
Rep Power: 1
Solved Threads: 0
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:
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
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
Similar Threads
- string arrays: storing input (C++)
- pass the value to the text box (JavaScript / DHTML / AJAX)
- How do you pass objects as arguements? (Java)
- C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays (C++)
- need help understandin how to pass by reference. (Java)
- need help in creating class string (C++)
Other Threads in the C Forum
- Previous Thread: Passing type as a parameter; generalised linked list in C?
- Next Thread: Need help on my assignment !!! - about file streams :((



Linear Mode