I have a function like this void tm(char ***path)
can u please tell how to change second character of string that I am getting in tm(char ***path)
function from some other function
int handle_process(struct connection *con_obj,char **path,char **request_type)
{
tm(&path)
}
void tm(char ***path)
{
**(*path 1)='x';
}
and print the updated path string in handle_process
function. I am getting segFault
CodePudding user response:
It is easier to understand if you split it into more than one operation (to be more difficult second character of the second string):
void tm(char ***path)
{
char **twoStars = *path;
char *singleStar = twoStars[1];
singleStar[1] = 'x';
}
void tm1(char ***path)
{
(*((*path) 1))[1] = 'z';
}
//or only pointer arithmetic
void tm2(char ***path)
{
*(*((*path) 1) 1) = '0';
}
//or only indexes
void tm3(char ***path)
{
path[0][1][1] = 'z';
}
int main(void)
{
char str1[] = "hello";
char str2[] = "world";
char **strings = (char *[]){str1, str2};
printf("string1 = `%s`, string2 = `%s`\n", strings[0], strings[1]);
tm(&strings);
printf("string1 = `%s`, string2 = `%s`\n", strings[0], strings[1]);
tm1(&strings);
printf("string1 = `%s`, string2 = `%s`\n", strings[0], strings[1]);
}
https://godbolt.org/z/o1aqvT913
and the result:
string1 = `hello`, string2 = `world`
string1 = `hello`, string2 = `wxrld`
string1 = `hello`, string2 = `wzrld`
EDIT (OP comments):
example of the "chained" calls to functions increasing level of indirection
void tm(char ***path)
{
*(*((*path) 0) 1) = '0';
}
void bar(char **ptr)
{
tm(&ptr);
}
void foo(char *str)
{
bar(&str);
}
int main(void)
{
char str1[] = "hello";
foo(str1);
printf("string1 = `%s`\n", str1);
}