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!

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";
	}
}
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 *.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

#include <iostream>
#include <string>

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
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.