Need help, Basic question

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 48
Reputation: atman is an unknown quantity at this point 
Solved Threads: 0
atman atman is offline Offline
Light Poster

Need help, Basic question

 
0
  #1
Jan 24th, 2009
Hello.,
Im very new to C++ but im trying to resolve a fragment of the following code:

  1. #include <iostream>
  2. using namespace std;
  3. const int NO_STEPS = 4;
  4.  
  5. void s(int* a, int i);
  6.  
  7. int main ( ) {
  8. int i, a[NO_STEPS] = {2, 4, 1, 3};
  9.  
  10. for ( i = 0; i < NO_STEPS; i++ )
  11. s( a, i );
  12.  
  13. for ( i = NO_STEPS - 1; i >= 0; i-- )
  14. cout << a[i] << ' ';
  15. cout << "Boom!\n";
  16.  
  17. return 0;
  18. }
  19.  
  20. void s(int* a, int i) {
  21. int* p;
  22. int x, n = NO_STEPS / 2;
  23.  
  24. if (i != n) {
  25. p = a + i; // be careful - pointer addition here
  26. x = *p;
  27. *p = a[n];
  28. a[n] = x;
  29. }
  30. }

to understand it better i split the code into 2 parts and simplified it abit so im working only on the first loop having the following code:

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void s(int* a, int i);
  5.  
  6. main()
  7. {
  8. int i, a[4] = {2,4,1,3};
  9.  
  10. for(i = 0; i < 4; i++)
  11. {
  12. s(a,i);
  13. printf("%d", a[i]);
  14. }}
  15.  
  16. void s(int *a, int i)
  17. {
  18. int *p; int x, n=2;
  19.  
  20. if(i != 2)
  21. {
  22. p = a + i;
  23. x = *p;
  24. *p = a[2];
  25. a[2] = x;
  26. }}

going through it on paper i dont fully understand the last line of function a[2] = x; we already know what the x is and we already stored it in a[2], why to restore the same value to x??? and my result was very different on paper then compilers, here are the arrays values i got after the loop is complete 2114, when the compiler got 1244, could anybody please let me know where my problem is? its driving me nuts.
Thank you very much
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,055
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Need help, Basic question

 
1
  #2
Jan 24th, 2009
The code doesn't store the value of x in a[2] before that line. What makes you think it does?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 48
Reputation: atman is an unknown quantity at this point 
Solved Threads: 0
atman atman is offline Offline
Light Poster

Re: Need help, Basic question

 
0
  #3
Jan 24th, 2009
Originally Posted by Rashakil Fol View Post
The code doesn't store the value of x in a[2] before that line. What makes you think it does?
when in the function p=a+i; does it mean p=0+0? since a points to the 1st element which is a 0 and i is also 0 in a first pass of the loop??? or it would be a=0 + 2(since its the first element's value)?
thank you
Last edited by atman; Jan 24th, 2009 at 4:23 pm. Reason: forgot
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,055
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Need help, Basic question

 
3
  #4
Jan 24th, 2009
Originally Posted by atman View Post
when in the function p=a+i; does it mean p=0+0?
Only if a and i are both zero. But a is not zero.

since a points to the 1st element which is a 0 and i is also 0 in a first pass of the loop???
No, the first element is a 2. (The array whose memory location is passed as the parameter 'a' has the elements 2, 4, 1, 3.) But you're not adding the first element -- you're adding the memory address of the first element to an integer i. The sum (a+i) will be the memory address of the element i units over from the element pointed at by 'a'. For example, (a+2) will be the memory address of the element 1, and *(a+2) will be the value 1.

or it would be a=0 + 2(since its the first element's value)?
thank you
What the ****? How could the line "p=a+i" end up assigning a value to a?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 48
Reputation: atman is an unknown quantity at this point 
Solved Threads: 0
atman atman is offline Offline
Light Poster

Re: Need help, Basic question

 
0
  #5
Jan 24th, 2009
Thank you so much for fast and detailed responce., so after statement p=a+i, does it mean that p equals 2 in the first pass? and does it mean that with statement x=*p means that x=2? i'm abit confused with these 3 lines...

thank you
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,055
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Need help, Basic question

 
2
  #6
Jan 24th, 2009
Originally Posted by atman View Post
Thank you so much for fast and detailed responce., so after statement p=a+i, does it mean that p equals 2 in the first pass?
No, the statement p=a+i changes the variable p so that it contains a memory address i units after a. There is no way p will equal 2, because 2 is an int, and p is an int*.

and does it mean that with statement x=*p means that x=2? i'm abit confused with these 3 lines...
If *p evaluates to 2, then that means x will contain 2. Here is a simple example:

  1. int array[] = {2, 3, 5, 7, 11};
  2. int* p = array; // p contains the memory address of array[0]
  3. int x = *p; // *p returns whatever's at the memory address stored in p. So it returns the value that's in array[0]. So it return 2. Thus, the value 2 gets assigned to the variable x.
  4. int* q = p + 2; // q contains the memory address 2 units after p.
  5. int y = *q; // *q is 5, so y gets assigned 5.
Last edited by Rashakil Fol; Jan 24th, 2009 at 6:19 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 327 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC