943,863 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 407
  • C++ RSS
Jan 24th, 2009
0

Need help, Basic question

Expand Post »
Hello.,
Im very new to C++ but im trying to resolve a fragment of the following code:

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atman is offline Offline
50 posts
since Oct 2008
Jan 24th, 2009
1

Re: Need help, Basic question

The code doesn't store the value of x in a[2] before that line. What makes you think it does?
Team Colleague
Reputation Points: 1135
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Jan 24th, 2009
0

Re: Need help, Basic question

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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atman is offline Offline
50 posts
since Oct 2008
Jan 24th, 2009
3

Re: Need help, Basic question

Click to Expand / Collapse  Quote originally posted by atman ...
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.

Quote ...
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.

Quote ...
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?
Team Colleague
Reputation Points: 1135
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Jan 24th, 2009
0

Re: Need help, Basic question

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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
atman is offline Offline
50 posts
since Oct 2008
Jan 24th, 2009
2

Re: Need help, Basic question

Click to Expand / Collapse  Quote originally posted by atman ...
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*.

Quote ...
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:

C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 1135
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: does anyone remember how to make the diamond pattern in c++?
Next Thread in C++ Forum Timeline: Reading/writing a CSV file into array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC