Pointer won't point at beginning address in array

This code is designed to count the words in a string, but that's not my concern right now. It's not finished yet because I ran into a problem I can't understand. Here's the code:
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
40
41
42
43
44
45
#include <iostream>
#include <cctype>

void wordCount(char *);

int main()
{
	const int SIZE = 100;
	char user_string[SIZE];

	std::cout << "Enter a string: ";
	std::cin.getline(user_string, SIZE);

	std::cout << "That string has ";
	wordCount(user_string);
	std::cout << " words.\n";

	return 0;


}


void wordCount(char *string3)
{
	std::cout << "Test 1 *string3 = " << *string3 << std::endl;
	std::cout << "Test 1 string3[0] = " << string3[0] << std::endl;
	int numWords = 0;
	while(*string3 != '\0')
	{
		std::cout << "Test 2 *string3 = " << *string3 << std::endl;
		string3++;
		std::cout << "Test 2 string3[0] = " << string3[0] << std::endl;
		if(*string3 == ' ' && isalpha(*(string3 - 1)))
			numWords++;
	}
	std::cout << "Test 3 *string3 = " << *string3 << std::endl;
	std::cout << "Test 3 string3[0] = " << string3[0] << std::endl;
	std::cout << "Test 4 string[1] = " << string3[1] << std::endl;
	std::cout << "Test 4 *(string3 - 2) = " << *(string3 - 2);
	if(*string3 != '\0')
		numWords++;
	std::cout << numWords;

}

I put a bunch of testing statements to see what the output would be at that current time in the loop and function. Here is the ouptut:
Enter a string: Hello
That string has Test 1 *string3 = H
Test 1 string3[0] = H
Test 2 *string3 = H
Test 2 string3[0] = e
Test 2 *string3 = e
Test 2 string3[0] = l
Test 2 *string3 = l
Test 2 string3[0] = l
Test 2 *string3 = l
Test 2 string3[0] = o
Test 2 *string3 = o
Test 2 string3[0] =
Test 3 *string3 =
Test 3 string3[0] =
Test 4 string[1] = ╠
Test 4 *(string3 - 2) = l0 words.
Press any key to continue . . .
This is my question. I always thought that typing an array name with the subscript of an element number would return the value in the position. But as you can see in the program, at the beginning string[0] points to the beginning address, but then when I later type string[0] it points to different elements in the array, like every time the loop iterates, string[0] becomes that element position. Why is that? I'm having trouble because after the loop iterates I am trying to work with the beginning address again, but when I use string[0], it is not the beginning address I'm working with, but actually an element outside the end of the array!Anybody understand what I am saying and know why this is happening? For some of the tests, you can see it didn't print out anything, and when I tested string[1] after the loop, it prints an unknown char, which I take as an indication it's working outside the loop-- shouldn't string[1] be the char 'e'?
Lines 32 increments string3. After each increment, string3[0] refers to a different character.
Topic archived. No new replies allowed.