This part of my program does not print anything on screen. Any suggestions?
It should just be an example of how simple arithmetic can be used on pointers.
Thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13
void example_Four() {
int *i, j[10];
double *f, g[10];
int x;
i = j;
f = g;
for (x = 0; x < 10, x++;) {
std::cout << i + x << " " << f + x << "\n";
}
}
Herbert Schildt has a bad reputation, [EDIT] and assuming that this is an example from one of his books [/EDIT], it's an example of one of the reasons why.
On line 10, there is a comma where there should be a semicolon. As it is now, x < 10 is evaluated, found to be true, and then that true value is discarded when in the condition of the for loop x is post-incremented. Post-increment returns the value of the operand before the increment, which in this case is 0. 0 implicitly casts to false, and thus your loop never runs.
Ignoring all of the other problems with this example, the simplest way to fix it would be to remove the last semicolon on line 10 and replace the comma with a semicolon.
As a first year CS student, the way you have implemented your pointers and array is somewhat vague to me. It would be helpful to consider this way of implementation using the address of the array.
@Alex1993, it is your choice which index of the array a[ ] you want to choose. You can write
ptr = &a[0];
and ptr will you give the address of the first element of the array.
Based on my current experience, I believe it is important to not modify addresses because you want your whole program to work with stable and fixed addresses of an array (if you implement your program like that).
Consider to read Bjarne stroustop (creator of c++) books, you might find the pdf versions for free. Also Walter Savitch books are well explained and detailed for a student.