Re: 2-dimensional arrays and functions

From: Micah Cowan (micah_at_cowan.name)
Date: 10/30/03


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


Relevant Pages

  • Re: Problem with large arrays
    ... >am trying to using an array of signals that is just slightly larger) for the ... location of this very large memory. ... so that each pointer points to one row. ... row data structure and make the pointer point to it. ...
    (comp.lang.vhdl)
  • Re: Out-of-bounds nonsense
    ... 6.5.6p8 of the C standard says about C pointer arithmetic. ... moving throught that array. ... The wording used in both standards makes sense only if the relevent ... pointer arithmetic within 'malloc'ed memory blocks (which naturally have no ...
    (comp.std.c)
  • Re: Problem with large arrays
    ... >>am trying to using an array of signals that is just slightly larger) for the ... > location of this very large memory. ... instead of signals I used variables and the snapshot was not any ... > so that each pointer points to one row. ...
    (comp.lang.vhdl)
  • Re: Creating and Accessing Very Big Arrays in C
    ... As for memory differences between storing multidimensional arrays ... memory a pointer takes up. ... if I wanted to store a 100x100x100 3D array ... If anyone knows of any good sparse matrix ...
    (comp.lang.c)
  • Re: [C]Understanding arrays/pointers
    ... > This is an array who's name is not a pointer: ... * a variable containing a memory address ... then Array decays int the memory address of its first element. ...
    (alt.comp.lang.learn.c-cpp)