Can someone explain to me why this doesn't work. I'm java developer and new to c/c++
As far as i understood pointer of array is actually pointer of arrays first element, is that correct?
void test(char *tmp)
{
test2(tmp);
//*tmp++ = '1';
//*tmp++ = '2';
*tmp++ = '3';
*tmp++ = '4';
*tmp = 0;
}
void test2(char *tmp)
{
*tmp++ = '1';
*tmp++ = '2';
}
int main(int argc, char **argv)
{
char tmp[5];
test(tmp);
printf("%s", tmp);
return 0;
}
only 34 gets printed. When i debug this code in function test2 pointer of tmp is incremente by one but back in function test after test2 is called, pointer of tmp is back to its initial value.
If i just put all of the code in single function like this, it works:
void test(char *tmp)
{
*tmp++ = '1';
*tmp++ = '2';
*tmp++ = '3';
*tmp++ = '4';
*tmp = 0;
}
Also what does the *tmp = 0 on the last line do. I copied it from some other code. Without it there is some junk on the end of the array.
*tmp = 0). 2. Make the code readable by usingtmp[0] =etc.testto depend ontest2. for example if i increment tmp intest23 times i want in test to just continue from forth. Is that possible?chars to fit null terminator as well.