pointers & mats

Just experimenting with pointers & arrays. When I increment ptr with ++ptr, I can predict which number in my array list will be dereferenced to and it works. This seems cumbersome though.How do I successfully access matrix elements without going one at a time like ++ptr, etc. I've tried pnt++2 etc., no dice.

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

#include<iostream>

using namespace std;

int main()
{
	int array[] = {3,2,5,14,7,8,9};
	int *ptr = array;
	int array_size = sizeof(array);
	int *temp = ptr;
	cout<<array_size<<endl;
	cout<<ptr<<endl;
	++ptr;
	++ptr;
	++ptr;
	cout<<endl;
	cout<<"dereference"<<*ptr<<endl;
	cout<<ptr<<endl;
	++ptr;
	cout<<ptr;
	ptr = temp; //reset pointer
	++ptr;
	++ptr;
	cout<<"dereference"<<*ptr<<endl;

	return 0;
}
Maybe this is what you had in mind...
1
2
3
4
5
6
	int array[] = {3,2,5,14,7,8,9};
	int *ptr = array;
	cout << "array[3] " <<  array[3] << endl;
	cout << "*(ptr+3) " <<  *(ptr+3) << endl;
	ptr += 3;
	cout << "*ptr     " <<  *ptr << endl;

array[3] 14
*(ptr+3) 14
*ptr     14

Last edited on
Note that you can use the same syntax for arrays and pointers to the first element in the array.

1
2
3
4
int array[] = {3,2,5,14,7,8,9};
int *ptr = array;
cout << "ptr[3] " <<  ptr[3] << endl;
cout << "*(array+3) " <<  *(array+3) << endl;
Last edited on
You may try this too, (it's tested in Visual C++ 2010 and cplusplus.com shell.)

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

using namespace std;

int main(){
	int array[] = {3,2,5,14,7,8,9};
	int *ptr = array;
	cout << "array[3] " <<  array[3] << endl;
	cout << "ptr[3]" <<  ptr[3] << endl;
	cout << "(ptr+3)[0]" <<  (ptr+3)[0] << endl;
}
Chervil this IS exactly what I had in mind. Its minimal but has so much information for learning points. The two boxes sum it up (as Peter's does as well). I wanted to verify what I was seeing:
My testing code results at the bottom

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>

using namespace std;

int main()
{
	int array[] = {3,2,5,14,7,8,9};
		int *ptr = array;
		cout << "array[3] " <<  array[3] << endl;
		cout << "*(ptr+3) " <<  *(ptr+3) << endl;
		cout<<"pointer : "<<ptr<<endl;
		cout<<"ptr + 3 : "<<ptr+3<<endl;
		cout<<"ptr + 3 : "<<ptr+3<<endl;
		cout<<"pointer : "<<ptr<<endl;
		ptr += 3;
		cout<<"+=pointer : "<<ptr<<endl;
		cout << "*ptr     " <<  *ptr << endl;


	return 0;
}


My test results were that ptr + 3 does not increment the pointer but still has access to the address at ptr+3. I tried several embodiments. I can increment the pointer addresses by incrementing the pointer: ++ptr, ++ptr, etc. How would I increment the pointer in this fashion but lets say by 2 or 3 at a time steps?
How would I increment the pointer in this fashion but lets say by 2 or 3 at a time steps?

The syntax for incrementing a pointer is just the same as for incrementing an integer (though how the compiler processes it 'behind the scenes' is different).

For example you could do ptr = ptr + 3;
or more concisely ptr += 3;

edit: I see you already did this at line 15 above.
Last edited on
thx!
Topic archived. No new replies allowed.