pointer ambiguity

I understand why array_size is relevant, but I don't understand why array_size doesn't cover array (array_size + array).Afterall array_size implies full inclusion. Whats going on with array and array_size? I got the loops working, but not comfortable with my knowledge base on this.

I know array pointer maps to 0; and number of elements start with 1 (pointer_array)

thx

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
#include<iostream>

using namespace std;

int main()
{
	int array[] = { 6, 3, 2, 9, 7, 1, 5, 4, 8 };
	int array_size = sizeof(array)/sizeof(array[0]);//how many elements
	
	//loops self-reset
	for(int *ctr_outer = array; ctr_outer<array_size + array ; ++ctr_outer)
		{
		cout<<"out ";
	for (int*ctr_inner = array; ctr_inner<array_size+array; ++ctr_inner)
					{
						cout<<"in ";

					}
			cout<<endl;


		}

		return 0;
}

Last edited on
Hi,


It works as expected for me, I had this output:


out in in in in in in in in in 
out in in in in in in in in in 
out in in in in in in in in in 
out in in in in in in in in in 
out in in in in in in in in in 
out in in in in in in in in in 
out in in in in in in in in in 
out in in in in in in in in in 
out in in in in in in in in in 


So there are 9 in, and 9 rows - which is consistent with 9 elements in the array, so I don't understand what the problem is?

I added some extra cout as a poor man's debugging. I recommend you try to use a real debugger - it's most instructive and essential skill to have.

Anyway, If you don't understand how the code works, try different things to demonstrate how it does work.


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
#include<iostream>

//using namespace std;

int main()
{
	int my_array[] = { 6, 3, 2, 9, 7, 1, 5, 4, 8 };  // not keen on variable names same as in std::
	int array_size = sizeof(my_array)/sizeof(my_array[0]);//how many elements
	
	std::cout << "Array size is " << array_size << "\n";
	std::cout << "The last value is " << *(my_array + array_size-1) << "\n\n";
	
	// I find it handy to prefix pointer types with p, I don't do it for other types though
	for(int *pElement = my_array; pElement < (array_size + my_array) ; ++pElement)
		{
		  std::cout <<  *pElement << " ";
	      
           // removed inner loop, it serves no purpose

		}
		
	std::cout << "\n\nWithout pointers " <<  "\n\n";	
	
	for (int MyCounter = 0; MyCounter < array_size ; ++MyCounter )
	    {
	       std::cout <<  my_array[MyCounter] << " ";
	    }

		return 0;
}



Array size is 9 
The last value is 8

6 3 2 9 7 1 5 4 8 

Without pointers 

6 3 2 9 7 1 5 4 8  


Hope this helps :+)
It does - thx for the reply. My question is why add array_size + my_array? Why not just array_size?

I got it working to, but I kind of cheated a bit by including the array_size+my_array from other code without really understanding why it works or why its the right size.

Iam using Eclipse IDE - do you think its debugger is alright to work with?

Hi,

My question is why add array_size + my_array? Why not just array_size?


If you were to print out the value of my_array, you would see that it is a pointer and it's value is the memory address. When one does pointer arithmetic, the compiler knows the size of 1 element, so when you add a number n to an address, it can calculate the address of the n'th element in the array. So my_array + array_size is the address of the last element in the array. Without that sum, we have 0 + array_size , which is certainly invalid.

So all that was for when you use pointers and pointer arithmetic. The second part of my code shows how to do it without pointers.

It is good to learn about raw pointers, especially for C Programming, but in C++ they are discouraged. One can do lots of things just using the STL containers and algorithms, if you really need a pointer, then consider smart pointers - read up about unique_ptr. Boost is really worth looking at, I am fooling around with their mpl and units stuff at the moment.


Iam using Eclipse IDE - do you think its debugger is alright to work with?


I am sure it will be fine. To get at it, either click the Debug button at the top right of the window, or use the menu: Window, Open Perspective, Debug. I have Eclipse Kepler (along with 3 other IDE's), so hopefully your layout is similar.

Regards :+)
Topic archived. No new replies allowed.