Re: 2-dimensional arrays and functions
From: Micah Cowan (micah_at_cowan.name)
Date: 10/30/03
- Next message: Micah Cowan: "Re: 2-dimensional arrays and functions"
- Previous message: Mike Wahler: "Re: 2-dimensional arrays and functions"
- In reply to: Jumbo: "Re: 2-dimensional arrays and functions"
- Next in thread: Jumbo: "Re: 2-dimensional arrays and functions"
- Reply: Jumbo: "Re: 2-dimensional arrays and functions"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 30 Oct 2003 11:53:51 -0800
"Jumbo" <(nospam)@12freeukisp.co.uk> writes:
> I don't know how I can get it into your two thick skulls
> look at this function Mike posted :
>
> int (*func(void))[DIM1][DIM2]
> {
> static int array[DIM1][DIM2] =
> {
> { 1, 2, 3, 4},
> { 5, 6, 7, 8},
> { 9, 10, 11, 12},
> {13, 14, 15, 16},
> };
>
> return &array;
> }
>
>
> I suppose you think this is wrong too.
> Now if you search around enough you will find thousands of C++ functions
> that return memory addresses.
The *BIG* difference here is that array is declared static. It is
statically allocated, and so its memory address is still valid
when it's returned, because array still exists after the function
is exited.
pArray in your example code was automatically allocated, and thus
its lifetime expired upon return.
> You obviously don't know the difference between a pointer and a memory
> address.
You're right, I don't. Neither do you, you just think you do.
> You seem to be confused by indirection
Nope.
> here's how it works:
>
> int*** return_Array(int dim1, int dim2){
>
> int** pArray = new int*[dim1];
>
> //new pointer to a pointer declared
> //pArray is the pointer to the fist element of the array
> //&pArray is the memory address of a pointer to the first element of the
> array
And is also a pointer to the pointer variable that contains a
pointer to the first element of the array.
Where's your quote from the Standard to back up your (implied)
assertion that &pArray is not a pointer?
Let's see what the Standard has to say about &pArray (quoting
from C++, since you like the new operator. This is 5.3.1#2):
"The result of the unary & operator is a pointer to its
operand."
Is that official enough for you?
-- Micah J. Cowan micah@cowan.name
- Next message: Micah Cowan: "Re: 2-dimensional arrays and functions"
- Previous message: Mike Wahler: "Re: 2-dimensional arrays and functions"
- In reply to: Jumbo: "Re: 2-dimensional arrays and functions"
- Next in thread: Jumbo: "Re: 2-dimensional arrays and functions"
- Reply: Jumbo: "Re: 2-dimensional arrays and functions"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|