### Pointers, Herber Schildt ... not working

Hey, guys!

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!

 ``12345678910111213`` `````` 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"; } }``````
There was a problem with the for loop, and also if you want to use the value as a number and not the address, then you have to use *.

 ``123456789101112131415161718192021222324252627282930313233343536373839`` `````` #include #include void example_Four() { int *i, j[10]; double *f, g[10]; int x; i = j; f = g; for (x = 0; x < 10; ++x) { // Address std::cout << i + x << " " << f + x << "\n"; } std::cout << "\n"; } void test_num(){ int *i, j[10]; double *f, g[10]; int x; i = j; f = g; for (x = 0; x < 10; ++x) { // Value std::cout << *i + x << " " << *f + x << "\n"; } } int main() { example_Four(); test_num(); return 0; }``````
Last edited on
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.

-Albatross
Last edited on
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.

 ``1234567891011121314151617181920`` ``````#include #include using namespace std; void test_num(){ int* ptr; int a[] = {200, 300, 400}; ptr = &a[1]; // &a[1] is the address of second element of a[]. cout << ptr << endl; // Address cout << *ptr << endl; // Value } int main() { test_num(); return 0; } ``````

Hope this helps!
Last edited on
Thank you very much! The comma is my fault because of lack of attention. Kourosh23 I need to ask why did you start from a[1] and not from a[0].

The program is meant to show how arithmetic pointers can be used to modify addresses.

The book is not that bad. It is a good introduction. I like books who dumb things down to be accessible to everyone.
@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.
Topic archived. No new replies allowed.