Public Group

# C++: Array of Pointers to Int or Array of Int Pointers?

This topic is 4124 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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
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?

##### Share on other sites
1. You would have to do:
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

##### Share on other sites
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]

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by JemburulaTheoretically, 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.

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 16
• 18
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633360
• Total Posts
3011524
• ### Who's Online (See full list)

There are no registered users currently online

×