i have a simple pointer to an array and i want to reverse that array, this is the code that i wrote:
void str_reverse(char *s) {
int l, r;
l = 0;
r = str_len(s) - 1;
char temp;
printf("length %i ", r);
while (l <= r) {
temp = *(s + l);
printf("\n%c [%i] | %c [%i]\n", temp, l, *(s + r), r);
*(s + l) = *(s + r);
*(s + r) = temp;
r--;
l++;
}
}
As you can see i use this fuction:
int str_len(char *p) {
int l = 0;
while (*(p + l) != '\0') {
// printf("%c %i", *(p + l),l);
l++;
}
return l;
}
I order to get the length of my array, this is my little main
int main() {
char *s = "Hallo world!";
str_reverse(s);
return 0;
}
And this is the output:
length 10
H [0] | d [10]
Bus error: 10
Now i tried to only print the values, so in the while loop of str_reverse i commented this two lines:
*(s + l) = *(s + r);
*(s + r) = temp;
And this is the output:
H [0] | ! [11]
a [1] | d [10]
l [2] | l [9]
l [3] | r [8]
o [4] | o [7]
[5] | w [6]
That is good! But how can i store them? Thank you in advance