Pointer beyond end of array

I found these informative videos, but I am bothered by the presence of a pointer beyond the end of an array. Is it common to see pointers go beyond scope and being used in calculations?
In static and dynamic arrays you can easily use the const for the elements or sizeof(array)/sizeof(type), so is there really a need to do this or should this just be avoided at all costs? I can easily see someone or myself accidentally accessing it.

I also learned here that if you take the address of a pointer to the beginning of an array & subtract that from one beyond the array, that the two address subtractions gives you the number of elements...pretty cool.

Any other very good beginners educational videos that you recommend? Would love a set of videos that work on small projects as well.


https://www.youtube.com/watch?v=GOCgl88hyzY

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
46
47
48
  #include <iostream>
using namespace std;

int main()
{
	const int NSTRINGS = 5;
	string texts[NSTRINGS] = {"h", "e", "l", "l", "o"};

	
	string* pTexts = texts;				//Same as below...points to beginning of array, 'h'
	//string* pTexts = &texts[0];		//Same as above...points to beginning of array, 'h'
	cout << *pTexts << endl;			//Prints 'h'
	pTexts += 3;						//Now points to 2nd 'l'
	cout << *pTexts << endl;			//Prints 2nd 'l'
	pTexts -= 2;						//Now points to 'e'
	cout << *pTexts << endl;			//Prints 'e'
	pTexts = texts;						//Reset pointer to start
	
	
	//string* pEnd = &texts[NSTRINGS-1]; //n-1, last element 5-1 = [4] = 'o'		DOES NOT WORK!!!!! SEE NEXT LINE!!!!
	string* pEnd = &texts[NSTRINGS];	//last element 5 = [5] = Beyond array scope, use this but DO NOT PRINT or READ from it!!!!!
	
	cout << "\n\n";
	while (pTexts != pEnd)
	{		
		cout << *pTexts;
		 pTexts++;
	}
	cout << endl;
	pTexts = texts;						//Reset pointer to start
	
	
	//pEnd now points to '&texts[NSTRINGS]", one boyond array scope, so 6-0 = 5 elements or address points.
	long elements = (long)(pEnd - pTexts);	
	cout << "Number of elements = " << elements << endl;	
	
	
	//What if we wanted to point to middle of array?
	pTexts = texts;						//Reset pointer to start
	pTexts += NSTRINGS/2;				//5/2 = 2 and disgard remainder 1															
	cout << *pTexts << endl;			//Prints 																													  
	
	return 0;
}



Last edited on
Yes, it's extremely common to have a pointer one beyond the end of the array - just don't try and dereference it!

Similarly the .end() iterators for c++ containers point one beyond the end of an array.

It's a good means of testing for the end of an array when you are iterating through it. Once your iterating pointer matches this you can stop the loop (as in your line 24).
I am not a fan, but you may as well get used to it, its a design choice that has pros and cons.
As noted the built in tools work off this design, so you are stuck with it.
That there is a well-defined 'address one beyond the end of an array' is not something that C++ invented. This design choice has been there from the early versions of C; C++ inherited it from C ('as close to C as possible, but no closer') and then went on to provide a close similarity between pointers and iterators.
Topic archived. No new replies allowed.