The result is tt. First I want to ask How? str+=3; makes is tract, but then
1. how a is t? does it happen from the original Abstract or from tract?
2. how b is t?
3. why does b needs * but a does not?
4. when the program cuts the word and shows everything after the third symbol for example and when it shows just the third symbol?
because str is a pointer to char (1 byte): +3 increments str 3 bytes. str points (pointer) now to the first "t" in "Abstract".
b=*(str++);
* gets the character where str +1 points to. this is the letter after "t" -> "r"
b contains after assignement with "=" the value of "r" (not the address (pointer))
a=(str+3);
a is a pointer and is assigned with the address of str+3, which means the address after "r" + 3 chars and this is the next "t"
str+=3; //Abstract - we cut the first three letters and we have tract
b=*(str++); // ++? so we take the first symbol of what we have left - so.. tract - t?
a=(str+3); // I dont even know why here is t.. from tract ... I thought it would be c?
cout<<a<<b;
I think whats confusing you is the post increment. In the line where "++" is seen, first the variable "str" is used, then after that the increment is applied. So...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
usingnamespace std;
char *a, b;
char *str="Abstract";
int main()
{
str+=3; // str is now pointing to the "t" in "tract"
b=*(str++); // this dereferences str to get "t", and assigns it to b.
// after this assignment, the pointer str is incremented
//to point to the "r" in "ract"
a=(str+3); // str+3 now points to "t", and that is now what 'a' points to
cout<<a<<b; // prints "tt"
return 0;
}
So line 10 of the above code can be re-written as the following two lines:
1 2
b = *str; // assign "t" to b
str++; // increment 'str' pointer so it now points to "r" in "ract"