Forgive my tedious question:Dynamical Multi-Dimensional Array 
Author Message
 Forgive my tedious question:Dynamical Multi-Dimensional Array

I` ve never asked anyone before...
but it`s time to do it.

Please

Anyone can answer me if this code work correctly in building 2-D
(or generally N-D) dynamic array?

int** x;

x = (int**) malloc ( row * sizeof ( int* ) );
assert ( x != NULL );

for ( i = 0 ; i < row ; i++ )
        {
            x[i] = (int*) malloc ( col * sizeof ( int ) );
            assert ( x[i] != NULL );
        }
then i refer to array x in this way

int y;

y = x[i][j]

when/where does it work? Anywhere? Anytime? AnyOS?

I know tha it`s a tedious question but i`m in trouble...

Thanks in advance

Fabricious (latin way of life)

Please remove NOSPAM from my e-mail address if you want
to mail me

*****************************************************************************
Fabrizio Badini
Mobile Services and  Radio                    
Mobile Services  
Planning Tools for Mobile Networks  (MR\M\P)

CSELT S.p.A.
10148 Torino (Italy) -  Via Reiss Romoli, n. 274
Tel. +39 11 2285417 - Fax  +39 11 2285582 - Lab. +39 11 2285443

*****************************************************************************



Sun, 19 Mar 2000 03:00:00 GMT  
 Forgive my tedious question:Dynamical Multi-Dimensional Array

Quote:

>I` ve never asked anyone before...
>but it`s time to do it.
>Please
>Anyone can answer me if this code work correctly in building 2-D
>(or generally N-D) dynamic array?
>int** x;
>x = (int**) malloc ( row * sizeof ( int* ) );
>assert ( x != NULL );
>for ( i = 0 ; i < row ; i++ )
>    {
>        x[i] = (int*) malloc ( col * sizeof ( int ) );
>        assert ( x[i] != NULL );
>    }
>then i refer to array x in this way
>int y;
>y = x[i][j]
>when/where does it work? Anywhere? Anytime? AnyOS?

Apart from the fact that a) there are some stylistic issues that
are not too pleasant imho and b) the topic is treated in the FAQ,
and c) this is not how you use assertions in production code,
this is one way to do it.

Kurt

--
| Kurt Watzka                             Phone : +49-89-2178-2781



Fri, 31 Mar 2000 03:00:00 GMT  
 Forgive my tedious question:Dynamical Multi-Dimensional Array

: >I` ve never asked anyone before...
: >but it`s time to do it.

: >Please

: >Anyone can answer me if this code work correctly in building 2-D
: >(or generally N-D) dynamic array?

: >int** x;

: >x = (int**) malloc ( row * sizeof ( int* ) );
: >assert ( x != NULL );

: >for ( i = 0 ; i < row ; i++ )
: >  {
: >      x[i] = (int*) malloc ( col * sizeof ( int ) );
: >      assert ( x[i] != NULL );
: >  }
: >then i refer to array x in this way

: >int y;

: >y = x[i][j]

: >when/where does it work? Anywhere? Anytime? AnyOS?

: Apart from the fact that a) there are some stylistic issues that
: are not too pleasant imho and b) the topic is treated in the FAQ,
: and c) this is not how you use assertions in production code,
: this is one way to do it.

: Kurt

: --
: | Kurt Watzka                             Phone : +49-89-2178-2781

        traditional approach to dynamic allocation
         ( multi-dimensional array ... )

        approach is effective for
        dynamic allocation of
                arrays ( matrices )
        of one or more dimensions
        ...

        also ...
        whether 'square' 'other-rectangular'
        or 'unusual' ( as 'half-diagonal' )
        or 'ragged' approach allows for
        precise,  effective allocation ...

/* command_line invocation of myprog00 with three arguments */

C:> myprog00 this00 that505 theother01

      argv[0]   argv[1]         argv[2]         argv[3]
0       'm'     't'             't'             't'
1       'y'     'h'             'h'             'h'
2       'p'     'i'             'a'             'e'
3       'r'     's'             't'             'o'
4       'o'     '0'             '5'             't'
5       'g'     '0'             '0'             'h'
6       '0'     '\0'            '5'             'e'
7       '0'                     '\0'            'r'
8       '\0'                                    '0'
9                                               '1'
10                                              '\0'

        /*  argv[0][1] >is< 'y'  */
        /*  argv[1][1] >is< 'h'  */
        /*  argv[1][6] >is< '\0'  */

--

Ralph Silverman



Sun, 02 Apr 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Dynamic Allocation of Multi-Dimensional Array (Simple Question)

2. Multi dimensional array question

3. Multi-dimensional array question.

4. Array of multi-dimensional arrays

5. Multi-Dimensional Array to Array Assignment

6. Multi-dimensional array

7. Has anyone iterated over a multi-dimensional array?

8. Multi-dimensional array using dynamic memory allocation

9. passing a multi-dimensional array

10. multi-dimensional array differences in MCpp & C#

11. Pointer to a multi-dimensional array?

12. Using multi-dimensional arrays

 

 
Powered by phpBB® Forum Software