I found something interesting, but could not explain it. I am writing a simple procedure to change a string. This works fine, no complaints.
My problem is in printf. When I print the original line separately, it prints correctly, but when I print the original line as the first argument, and the function call is treated as the second, both are displayed as the inverse.
Exit:
|| abcdthelgko ||
Orig Str: | okglehtdcba |, Rev Str | okglehtdcba |
code:
char* ReverseStr(char* str, int len)
{
char *start = &str[0], *end = &str[len-1];
char temp;
while(start < end)
{
temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
return str;
}
int main()
{
char str_unique[] = "abcdthelgko";
int str_unique_len = sizeof(str_unique)/sizeof(str_unique[0]);
printf("\n || %s || \n", str_unique);
printf("Orig Str: | %s |, Rev Str | %s |\n", str_unique, ReverseStr(str_unique, str_unique_len-1));
return 0;
}
* : *
2 ,
1. Printf
2. , printf,
, , . , :
================== ======================
i = 5, changed i = 2, again i= 2
changed i = 2, i= 2
i = 5, changed i = 2
================== ======================
int* change (int* addr);
int main()
{
int i;
i=5;
printf("\n i = %d, changed i = %d, again i= %d \n ", i, *change(&i), i);
i=5;
printf("\n changed i = %d, i= %d \n", *change(&i), i);
i=5;
printf("\n i = %d, changed i = %d \n", i, *change(&i));
return 0;
}
int* change (int* addr)
{
*addr = 2;
return (addr);
}