Can't change string given to function - c

Once again, I have stumbled across another unexpected occurrence.
In the following code, it seems that if I use changeStr() to change a string, from the function's perspective it has changed, but it has not changed from the program's perspective.
However if I don't use changeStr() and set the string myself, it seems to work.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* set string to newStr */
void changeStr(char *string, char *newStr) {
string = realloc(string, strlen(newStr) + 1);
memcpy(string, newStr, strlen(newStr) + 1);
printf("String from function=%s\n", string);
}
int main() {
char *str = NULL;
changeStr(str, "Hello world!");
printf("String=%s\n", str);
/* set string without function */
char *test = NULL;
char *newStr = "hello world";
test = realloc(test, strlen(newStr) + 1);
memcpy(test, newStr, strlen(newStr) + 1);
printf("NewStr=%s\n", test);
free(str);
free(test);
return 0;
}
Output:
String from function=Hello world!
String=(null)
NewStr=hello world
I know that function parameters in C are duplicated into a new variable for the function, but only the pointer is duplicated - not the string itself - so what's going on here?
P.S. I am no multi-year experienced programmer, so not sure if something is wrong.

The realloc() call as you are using it creates a new object and returns the pointer to it, which gets assigned into the string local variable in changeStr().
If you want the caller function (main()) to have access to that newly allocated object, then the callee function (changeStr()) needs to pass the new pointer back to the caller (e.g. as a return value). For example:
char *changeStr(char *string, char *newStr) {
string = realloc(string, strlen(newStr) + 1);
memcpy(string, newStr, strlen(newStr) + 1);
printf("String from function=%s\n", string);
return string; // <-- return the updated string object to caller
}
int main() {
char *str = NULL;
str = changeStr(str, "Hello world!");
printf("String=%s\n", str);

Related

Why isn't this function(char*) pass by ref?

Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void set(char* str){
str = malloc(10);
strcpy(str, "dog");
printf("\nstr = %s", str);
}
int main(){
char* s;
set(s);
printf("\n%s", s);
return 0;
}
Here's what I want to print out:
str = dog
dog
Here's what actuall gets printed out:
str = dog
(null)
Why is this? What I think I'm doing is passing an uninitalized pointer that then gets assigned a block of memory in set(), which then gets "dog" written into. What's actually going on?
There is no pass-by-reference in C. But pointers and indirection gives a facility to mimic that indirectly.
In your case, what is sent from main() to set() is the address in variable 's'. It
will have that value till malloc() statement executes. After that, str will have whatever address is returned by malloc().
When the same thing is expected in main, what should have been passed is the address
of 's' rather than what address it holds (like some of the examples above).
C is a pass-by-value language. There is no way to pass something by reference except explicitly. For your case that means expecting a pointer-to-a-pointer:
void set(char **str)
{
*str = malloc(10);
strcpy(*str, "dog");
printf("str = %s\n", *str);
}
And calling with the address of the pointer you want to 'fill in':
int main(void)
{
char *s;
set(&s);
printf("%s\n", s);
return 0;
}
Why isn't this function(char*) pass by ref?
C doesn’t have pass-by-reference. Everything is pass-by-value – it’s just that some values are also pointers to other values. Your uninitialized variable s is read when you call set(s) (which is undefined behaviour) in order to provide a value for its parameter str, then str = malloc(10) throws that value away to assign a new value to the local str.
You can pass a pointer to the pointer:
void set(char** str){
*str = malloc(10);
strcpy(*str, "dog");
printf("\nstr = %s", *str);
}
int main(){
char* s;
set(&s);
printf("\n%s", s);
return 0;
}
or return a pointer:
char* set(void) {
char* str = malloc(10);
strcpy(str, "dog");
printf("\nstr = %s", str);
return str;
}
int main(){
char* s = set();
printf("\n%s", s);
return 0;
}
You are passing an unasigned pointer to your set function, here a copy of the pointer is made and you use malloc on that copy. The original pointer in main is never updated with the new memory address.
To achieve what you want could be done this way:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void set(char** str){
*str = malloc(10);
strcpy(*str, "dog");
printf("\nstr = %s", *str);
}
int main(){
char* s;
set(&s);
printf("\n%s", s);
return 0;
}
I used a pointer to a pointer in set, and pass the memory address of the pointer s to it.

Memory management in replace function

I'm trying to make a replace function in C. I know there are many out there that I could copy, but I decided to make my own function in order to practice.
However, I'm stuck at this:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void replace_content(char *rep, char *with, char **text) {
int len_rep = strlen(rep);
int len_with = strlen(with);
char *p = *text;
int new_text_size = 0;
char *new_text = malloc(new_text_size);
do {
if (!strncmp(p, rep, len_rep)) {
new_text_size += len_with;
new_text = (char *) realloc(new_text, new_text_size + 1);
strcat(new_text, with);
p += len_rep;
} else {
new_text_size++;
new_text = (char *) realloc(new_text, new_text_size);
new_text[new_text_size-1] = *p;
p++;
}
} while (*p != '\0');
*text = malloc(new_text_size);
strcpy(*text, new_text);
}
int main() {
printf("Testing a replace function:\n");
char *text =
"<serviceName>\n"
" <label1>a</label1>\n"
" <label2>b</label2>\n"
" <label3>c</label3>\n"
"</serviceName>\n";
printf("Before replace:\n%s", text);
replace_content("serviceName>", "serviceNameResponse>", &text);
printf("After replace:\n%s", text);
return 0;
}
This is the output I'm seeing so far:
Testing a replace function:
Before replace:
<serviceName>
<label1>a</label1>
<label2>b</label2>
<label3>c</label3>
</serviceName>
After replace:
<0�serviceNameRespons
<label1>a</label1>
<label2>b</label2>
<label3>c</label3>
</serviceNameResponse>
My guess is that I'm doing something wrong with dynamic memory, but the more I look into my code the more confused I am.
These two statements are problematic:
new_text = (char *) realloc(new_text, new_text_size + 1);
strcat(new_text, with);
The first problem is that you should never assign back directly to the pointer you reallocate. That is because realloc may fail and return NULL, making you lose the original pointer.
The second problem is that new_text doesn't initially point to a null-terminated string, which makes the call to strcat undefined behavior.
There is also a problem in the else branch:
new_text = (char *) realloc(new_text, new_text_size);
new_text[new_text_size-1] = *p;
Besides the same problem with reassigning back to the pointer being reallocated, you don't terminate the string in new_text.
May the reason is malloc(0) in line 10 char *new_text = malloc(new_text_size);.
The malloc() function allocates size bytes and returns a pointer to
the allocated memory. The memory is not initialized. If size is 0,
then malloc() returns either NULL, or a unique pointer value that
can later be successfully passed to free().
I suggest using char *new_text = NULL; instead.

String made NUL after function cal

I am trying to recreate the C++ function std::string.pop_back in C, where the last non-NUL character of the input string is popped off. In the pop_back function below the variable out has the desired output of the function, but trying to reassign *string to be out makes *string be the correct value before the function closes (for example, if I were to print string* at the end of the function, it would output Hello World), but in main(), after pop_back() is called, string is output as blank. What am I doing wrong in reassigning string?
#include <stdio.h>
#include <string.h>
void pop_back(char** string) {
size_t stringLen = strlen(*string);
char out[stringLen];
strxfrm(out, *string, stringLen);
// Here *string == "Hello World!"
*string = out;
// Here *string == "Hello World"
}
int main(int argc, char* argv[]) {
char* string = "Hello World!";
printf("Initial string: %s\n", string);
pop_back(&string);
printf("After pop_back: %s\n", string);
return 0;
}
// Output:
// $ ./pop_back_test
// Initial string: Hello World!
// After pop_back:
// Expected output:
// $ ./pop_back_test
// Initial string: Hello World!
// After pop_back: Hello World
With your code
char out[stringLen];
...
*string = out;
You "return" (assign to a parameter) a pointer to a "local variable", i.e. a pointer to an object with automatic storage duration, which's life time ends at the end of the function. Accessing an object beyond its lifetime is undefined behaviour.
You probably have to dynamically allocate a new string, which you can then manipulate and use after function execution has ended. For example:
if (!(*string) || !(**string)) {
// decide what to do with NULL or empty input strings.
...
}
else {
size_t stringLen = strlen(*string);
char* out = malloc(stringLen);
memcpy(out, *string, stringLen);
out[stringLen-1] = '\0';
}
*string = out;
Don't forget to free allocated memory afterwards. BTW: if you operate on a copy, I'd suggest to return the (new) copy and leave the input pointer as is, i.e. I'd change the prototype to
char* pop_back(char* string) {
...
return out;
}
As I step through your code, the message in my debugger indicates on line...
*string = out;
...that *string is over array bounds, failing the assignment. *string points to a string literal which means among other things it cannot be modified. Attempting to modify its content will always fail.
There are several ways this can be remedied.
One way is to modify your pop_back() function to accept a char *, and return a char *, allowing you to pass in an immutable string and return the resulting string.
Suggested implementation:
char * pop_back(char *string)
{
size_t stringLen = strlen(string)+1;//add one for NULL
char *out = calloc(stringLen,1);//create space for new string
strxfrm(out, string, stringLen);
// Here *string == "Hello World!"
return out;//return string
// Here *string == "Hello World"
}
int main(int argc, char* argv[])
{
char *string1 = "Hello World!"; // source string
char *string2 = {0}; // container for local result
printf("Initial string: %s\n", string1);
string2 = pop_back(string1); // get local result
printf("After pop_back: %s\n", string2);// use local result
free(string2); // free local result
return 0;
}

fprint cannot print concatenated string referenced by pointer returned from function

I have the following program:
#include <stdio.h>
#define MAXLEN 100
char *my_strcat(char *strp1,char *strp2) {
char str[MAXLEN], *strp;
strp = str;
while (*strp1 != '\0') {
*strp++ = *strp1++;
}
while (*strp2 != '\0') {
*strp++ = *strp2++;
}
*strp = '\0';
strp = str;
return strp;
}
void test_strcat(void) {
char *strp1, *strp2, *strp3, str1[MAXLEN], str2[MAXLEN];
printf("Testing strcat! Give two strings:\n");
gets_s(str1, sizeof(str1));
gets_s(str2, sizeof(str2));
strp1 = str1;
strp2 = str2;
strp3 = my_strcat(strp1, strp2);
printf("Concatenated string: %s", strp3);
}
int main(void) {
test_strcat();
}
The function char *mystrcat is supposed to concatenate two strings, and I test it with
test_strcat. The program runs without errors but instead of printing the concatenated string a smiley symbol is printed. I have gone through the program with debugging and it
appears that the result sent back by my_strcat is the correct string. However, when
going into the last line where strp3 is supposed to be printed it appears red in the
debugging tool, implying that its value is about to change. After the printf call, strp3
no longer points to the concatenated string. Anyone knows what could be causing this error?
Here is the problem:
char str[MAXLEN], *strp;
strp = str; // str is a local variable
...
return strp; // <<== WRONG!!!
Since str is a local variable that disappears as soon as you return, the value pointed to by strp becomes invalid the instance the caller gets the control back.
Use malloc instead of allocating memory in the automatic storage area (i.e. on the stack) will fix this problem:
char *str = malloc(strlen(strp1)+strlen(strp2)+1);
char *strp = str;
I suggest you 2 ways as following.
first,
char *my_strcat(char *strp1,char *strp2) {
static char str[MAXLEN * 2]; /* from char str[MAXLEN] */
second,
char *my_strcat(char *strp1,char *strp2) {
char *str = malloc(strlen(strp1) + strlen(strp2) + 1);
because in my_strcat function, you allocated the str as auto variable.
When my_strcat function is finish, str will be freed.

How to set pointer reference through a function

In C, I am trying to set a pointer's value by sending it to a function, but the value wont change outside of the function. Here is my code:
#include <stdio.h>
void foo(char* str) {
char* new_str = malloc(100);
memset(new_str, 0, 100);
strcpy(new_str, (char*)"new test");
str = new_str;
}
int main (int argc, char *argv[]) {
char* str = malloc(100);
memset(str, 0, 100);
strcpy(str, (char*)"test");
foo(str);
printf("str = %s\n", str);
}
I want to print out:
str = new test
but this code prints out:
str = test
Any help will be appreciated. Thanks in advance.
There is no pass-by-reference in C. If you provide str as the argument to a function in C, you are always passing the current value of str, never str itself.
You could pass a pointer to str into the function:
void foo(char** pstr) {
// ...
*pstr = new_str;
}
int main() {
// ...
foo(&str);
}
As Eiko says, your example code leaks the first memory allocation. You're no longer using it, and you no longer have a pointer to it, so you can't free it. This is bad.
You need to use pointer to the pointer, untested:
#include <stdio.h>
void foo(char** str)
{
char* new_str = malloc(100);
memset(new_str, 0, 100);
strcpy(new_str, (char*)"new test");
if (str) { /* if pointer to pointer is valid then */
if (*str) /* if there is a previous string, free it */
free(*str);
*str = new_str; /* return the string */
}
}
int main (int argc, char *argv[])
{
char* str = malloc(100);
memset(str, 0, 100);
strcpy(str, (char*)"test");
foo(&str);
printf("str = %s\n", str);
}
You are just reassigning a pointer, which is a local variable in foo.
If you want to copy the string, use strcpy(str, new_str);
You could pass a reference to the pointer instead and reassign, but this can easily lead to memory leaks and is hard to maintain.
Edit: For the pseudo pass by reference see the answer by Steve.
I did it this way by returning the pointer from the function. There is no reason to use malloc in this case, so you don't have to worry about freeing.
gcc 4.4.3 c89
char* print_test(char *str)
{
char *new_str = "new_test";
printf("new_str [ %s ]\n", new_str);
str = new_str;
return str;
}
int main(void)
{
char *str = "test";
printf("str [ %s ]\n", str);
str = print_test(str);
printf("str [ %s ]\n", str);
return 0;
}

Resources