| | |
Need help, Basic question
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 48
Reputation:
Solved Threads: 0
Hello.,
Im very new to C++ but im trying to resolve a fragment of the following code:
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:
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
Im very new to C++ but im trying to resolve a fragment of the following code:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; const int NO_STEPS = 4; void s(int* a, int i); int main ( ) { int i, a[NO_STEPS] = {2, 4, 1, 3}; for ( i = 0; i < NO_STEPS; i++ ) s( a, i ); for ( i = NO_STEPS - 1; i >= 0; i-- ) cout << a[i] << ' '; cout << "Boom!\n"; return 0; } void s(int* a, int i) { int* p; int x, n = NO_STEPS / 2; if (i != n) { p = a + i; // be careful - pointer addition here x = *p; *p = a[n]; a[n] = x; } }
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)
#include<iostream> using namespace std; void s(int* a, int i); main() { int i, a[4] = {2,4,1,3}; for(i = 0; i < 4; i++) { s(a,i); printf("%d", a[i]); }} void s(int *a, int i) { int *p; int x, n=2; if(i != 2) { p = a + i; x = *p; *p = a[2]; a[2] = x; }}
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
•
•
Join Date: Oct 2008
Posts: 48
Reputation:
Solved Threads: 0
•
•
•
•
The code doesn't store the value of x in a[2] before that line. What makes you think it does?
thank you
Last edited by atman; Jan 24th, 2009 at 4:23 pm. Reason: forgot
Only if a and i are both zero. But a is not zero.
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.
What the ****? How could the line "p=a+i" end up assigning a value to a?
•
•
•
•
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
•
•
•
•
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?
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...
C++ Syntax (Toggle Plain Text)
int array[] = {2, 3, 5, 7, 11}; int* p = array; // p contains the memory address of array[0] 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. int* q = p + 2; // q contains the memory address 2 units after p. int y = *q; // *q is 5, so y gets assigned 5.
Last edited by Rashakil Fol; Jan 24th, 2009 at 6:19 pm.
![]() |
Similar Threads
- Basic C Question (C)
- Basic Variables Question (Visual Basic 4 / 5 / 6)
- A basic question about architecture (Windows NT / 2000 / XP)
- Basic Question (Visual Basic 4 / 5 / 6)
- A Basic Question. . . (Windows 95 / 98 / Me)
- Html basic question "creating a board thing" (HTML and CSS)
Other Threads in the C++ Forum
- Previous Thread: does anyone remember how to make the diamond pattern in c++?
- Next Thread: Reading/writing a CSV file into array
Views: 327 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






