I want to modify a string inside the function. My code is pretty easy:
#include <stdio.h>
void doSomething ( char **string )
{
sprintf(string,"some string");
}
int main ( void )
{
char *origString = NULL;
doSomething ( &origString );
printf ( "%s\n", origString );
return 0;
}
But this code doesn't work. I would really appreciate an explanation more than a solution, but solution is also welcome.
I really need to use sprintf function, because the idea is to add some other strings with %s.
Thanks!
Solved!
#include <stdio.h>
void doSomething ( char **string )
{
*string = malloc(strlen("some string") + 1);
sprintf(*string,"some string");
}
int main ( void )
{
char *origString = NULL;
doSomething ( &origString );
printf ( "%s\n", origString );
free(origString); // Previously malloced in doSomething
return 0;
}
sprintf
takes a char*
as its first argument, not a char**
, so it should be sprintf(*string,"some string");
. However *string
will be NULL
at that point, so that would still not work.
If you want the memory for your string to be allocated by the doSomething
function, you will need to allocate the memory in that function. sprintf
will not allocate any memory for you. So before you'd call sprintf
, you'd need to allocate memory using malloc
and make *string
point to that memory. You should also be aware that users of your function then needs to free the memory once they're done with it.
This should work
#include <stdio.h>
void doSomething ( char *string )
{
sprintf(string,"some string");
}
int main ( void )
{
char origString[200];
doSomething ( origString );
printf ( "%s\n", origString );
return 0;
}
this passes in a pointer to the first character in the string (array of 200 characters). The contents of this array can then be modified.
EDIT
With malloc
#include <stdio.h>
void doSomething ( char **string )
{
const char * const x = "Some string ksdskjkj";
*string = malloc(strlen(x) + 1 /* For the null characcter */); // Now origString will also point to this dynamic memory
sprintf(*string,"some string");
}
int main ( void )
{
char *origString = NULL;
doSomething ( &origString );
printf ( "%s\n", origString );
free(origString); // Previously malloced in doSomething
return 0;
}
Ed Heal's solution is correct but to keep it inline with how you were originally going you could have achieved the same result with this (albeit with a couple of warnings depending on your compiler settings):
#include <stdio.h>
void doSomething ( char **string )
{
sprintf(string,"some string");
}
int main ( void )
{
char * origString[200];
doSomething ( origString );
printf ( "%s\n", origString );
return 0;
}
In C strings are literally arrays of characters. You were trying to pass a null pointer to the doSomething(). doSomething ( char **string ) takes an address to a char pointer so you don't need to pass it the address of origString (ie &origString ). All you need is to pass it the dereferenced pointer via doSomething ( origString ).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.