understanding a simple pointers code

#include <iostream>


using namespace std;

void main()
{ int *p;
int *pa;
pa=new int[100];
for (int i=0;i<100;i++)
pa[i]=2*i;

p=pa;
cout<<p[0]<<endl;

p=p+10;
cout<<*p<<endl;

p++;
cout<<p[0]<<endl;

p=p-3;
cout<<p[0]<<' '<<p[3]<<endl;

for (p=pa;p<pa+100;p=p+10)


cout<<*p<<endl;



delete [] pa;




}

i am studying pointers and im really not getting the last for loop written in the code..
The values displayed by the compiler for the whole code is
0
20
22
16 22
0 (i guess the last looping starts here)
20
40
60
80
100
120
140
160
180

I don't get how these are the values. Please help me.Thank you!
I expect you don't understand pointer arithmetic.

Please read about pointers: http://www.cplusplus.com/articles/EN3hAqkS/

Pointer arithmetic is part way down, but read the first bit as well so that you understand pointers.
@Moschops
I just read your article, and it was very good and informative. But I have an issue. Why do you prefer to write pointers as int* a; instead of int *a? The reason I ask is because I've been trying to learn different things about pointers and the latest thing that I read was that you don't actually create an int pointer, you're dereferencing the int variable you just created so that it's implied when you use it, you're actually using the memory address, i.e. the pointer.

Either way, it doesn't matter in basic examples, but let's say you're creating two pointers, int* a, b;, the code is actually very confusing since you're creating a as a pointer, but b is just a regular int. If you were to write it as int *a, b; it's easier to tell that you're creating two different kinds of variables.

I know I'm partially off topic, but maybe you can clear up some of the things that I don't understand or that I was actually wrong about.

@gethelpcpp123
Sorry for interrupting, but I've been having issues with pointers for years now, it's not an easy thing, atleast not for me. Just remember that pointer isn't an actual datatype, it just simply points to a datatype. When using it with arrays, look at an array as sequential chucks of memory. An array of 100 elements will be sequentially 0-99 memory blocks. You can access each memory block with a pointer.

When you create the pointer and assign it to the array, the pointer points to the very first element. You can increment the pointer, just like you could an int, and it will point to the very next element.

Hope that helps get you on track.
@Volatile Pulse
There was a discussion about where to place the * not long ago: http://cplusplus.com/forum/general/73462/
Why do you prefer to write pointers as int* a; instead of int *a?

It's a reflection of how I think about it. int* is the type, a is the name.

latest thing that I read was that you don't actually create an int pointer,
That makes no sense. Here is an example of creating an int pointer.

1
2
int* a; // This creates an int pointer. It is 4 (or 8) bytes in size.
int *b; // This creates an int pointer. 


Just remember that pointer isn't an actual datatype

I disagree. They exist in memory, commonly taking up 4 (or 8) bytes. If they aren't a datatype, how could they take up memory? How could they exist? How could the compiler know to stop you trying to assign a pointer-to-double to a pointer-to-int?
Last edited on
I had completely missed that discussion. And I'm actually going to be posting a thread about iterators and pointers here shortly. I'm still very confused on a lot of things when it deals with pointers.
Remember, an iterator is not the same thing as a pointer.

Having read the article, what do you still not understand about pointers? What needs improving in it to be clearer?
Last edited on
Reading this reference on iterators: http://www.cplusplus.com/reference/std/iterator/

It says
In C++, an iterator is any object that, pointing to some element in a range of elements (such as an array or a container), has the ability to iterate through the elements of that range using a set of operators (at least, the increment (++) and dereference (*) operators).


To me, that sounds like an iterator is exactly a pointer.

As for your article, I didn't really see anything that stuck out as not being understood, but time and time again, I see that char* isn't a character pointer, it's a character, but you're creating the variable as a specific pointer. I know this is a huge preference thing. Maybe I still don't have a solid understanding of it (which is completely possible) but I'm trying to grasp everything at once. I did learn that when you reference a variable, it actually returns the memory location (which I learned from your article) and makes a lot more sense when I pass arguments by reference.

I think I simply need to practice with them more. They kind of scare me in the sense I have a difficult time understanding how they'd work, why I'd need them in my code, etc.
To me, that sounds like an iterator is exactly a pointer.

All pointers are iterators (in that, you can apply ++ and * to them), but not all iterators are pointers.

Imagine I create an object that accepts the ++ and * operator to iterate through a range, and also contains lots of other things. Is it a pointer? No. It's a new class object I created. Is it an iterator? Yes. So here is an object that is NOT a pointer but IS an iterator. Clearly, an iterator and a pointer are not the same thing.


char* isn't a character pointer, it's a character, but you're creating the variable as a specific pointer.

That's just plain wrong. char* IS a pointer. It is NOT a character. I think this is the foundation of your misunderstanding. A pointer is a complete object in its own right. It takes up memory. It has a value. The value of an int-pointer is NOT an int. The value of a char-pointer is NOT a char.

When I do this:
char* x;
I have NOT created a character.
Last edited on
Topic archived. No new replies allowed.