int (*ptr)[10];
is the proper declaration, i.e. ptr here is a pointer to an array of 10 integers. Note that this is different from
int *ptr[10];
which would make ptr the name of an array of 10 pointers to type int.
So that seems fairly straight forward... first is a pointer to an array that holds ints and the second is an array that holds pointers to ints. I've been playing around with the difference but I can't quite seem to see it in actual practice [confused]. Theoretically, if I had an array of 10 pointers to ints shouldn't I be able to say:
int *ptr[10];
*ptr = &someInt
It doesn't let me do that though?
Another thing I've been wondering is when you delete dynamic arrays with 'delete [] myArray;' how does it know how how many elements are in the array? Does it record how much you new'ed it with?
C++: Array of Pointers to Int or Array of Int Pointers?
Hello,
Man it must get so annoying... it seems like the questions I ask are so simple and alike some times but I can't quite get my head around it... it's almost as if sometimes you have to see someone actually say it/reconfirm what you're trying to understand before you can move on. Basically I've been reading Ted Jensen's C-based article on Pointers. In it, it says
1. You would have to do:
or
2. There is a 16 byte header on the front of each new'd allocation with information on the allocation.
Dave
ptr = &someInt // ptr is the first int* in the array. *ptr gets you the integer // being pointed at. Which means you could do *ptr = someInt.
or
ptr[x] = &someInt // For a 'ptr' other than the first one.
2. There is a 16 byte header on the front of each new'd allocation with information on the allocation.
Dave
Thanks for the quick reply. I think I see where I'm making the mistake. I started writing more to this post but it made me think a bit more... I think I understand now. [smile]
the truth is that you don't need to know the array syntax. Use std::vector for your container needs. If you really need a block of memory just new/malloc it and use it through a pointer.
Quote:Original post by Jemburula
Theoretically, if I had an array of 10 pointers to ints shouldn't I be able to say:
int *ptr[10];
*ptr = &someInt
It doesn't let me do that though?
No, that's fine. '*ptr' is equivalent to 'ptr[0]', so that puts the pointer to someInt into the first element of the array. Are you sure someInt is an int (and not itself a pointer-to-int)? ;s
Quote:Another thing I've been wondering is when you delete dynamic arrays with 'delete [] myArray;' how does it know how how many elements are in the array? Does it record how much you new'ed it with?
Implementation-defined. Dave's post describes one possibility. It will of course have to track the information *somewhere*, but it doesn't have to be anywhere near the actual array allocation.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement