Question about code

Hi, I have this problem with char *s

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 #include <iostream>
using namespace std;

  char *a, b;
char *str="Abstract";

 int main()
 {
     str+=3;
     b=*(str++);
     a=(str+3);
     cout<<a<<b;

       return 0;
} 


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?

Thanks a lot.
Last edited on
closed account (48bpfSEw)
char *a, b;

a is a pointer to char
b is a char

char *str="Abstract";

str is a pointer to char

str+=3;

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"
I don't understand.

how I see it:
1
2
3
4
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;
closed account (48bpfSEw)
http://www.cplusplus.com/doc/tutorial/pointers/
Ok, i will read it.
I have another question.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

using namespace std;


  struct S
  {
      int data;
      S *next;
  };
  S *p, *q;

  int main ()
  {
      p=new S;
      p->data=13;
      p->next=NULL;
      q=p;
      p=new S;
      p->data=23;
      p->next=q;
      cout<<p->next->data;

       return 0;
}


what is the right value to look at? 23 or 13?
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>
using namespace 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" 



what is the right value to look at? 23 or 13?


I dont understand this second question of yours.
Last edited on
Topic archived. No new replies allowed.