How access alloc'ed string as array? 
Author Message
 How access alloc'ed string as array?

I would like to allocate a block of memory from the heap and then
treat it as a two-dimensional array.  I know it's easy, but I just
can't seem to get the syntax right.  My compiler manual offers no
help.  Thanks


Fri, 27 Mar 1998 03:00:00 GMT  
 How access alloc'ed string as array?

Quote:

>I would like to allocate a block of memory from the heap and then
>treat it as a two-dimensional array.  I know it's easy, but I just
>can't seem to get the syntax right.  My compiler manual offers no
>help.  Thanks

Several techniques for this are demonstrated in the comp.lang.c faq.
You can d/l it by ftp from rtfm.mit.edu /pub/usenet/comp.lang.c.

--
John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:



Fri, 27 Mar 1998 03:00:00 GMT  
 How access alloc'ed string as array?

Quote:

>I would like to allocate a block of memory from the heap and then
>treat it as a two-dimensional array.  I know it's easy, but I just
>can't seem to get the syntax right.  My compiler manual offers no
>help.  Thanks

 The following code is an example of how to use a tabl of string.
Note: the number of string is set by the MAX_ELE const, and can only be changed by
changing it's value and then recompiling.

#include <stdlib.h>
#include <string.h>

#define  MAX_ELE        10

int main(void)
{
        int             i;
        char    *Tbl[MAX_ELE];

        // Alloc mem for MAX_ELE arrays
        for( i = 0; i < MAX_ELE; i++ )
        Tbl[i] = (char *) calloc( i * 10, sizeof(char) );

        // Put some junk in array #4
        strcpy( Tbl[3], "This is trash..." );

        // Free mem used by MAX_ELE arrays
        for( i = 0; i < MAX_ELE; i++ )
                free( Tbl[i] );

        return 0;

Quote:
}

  If this is not exactly what you wanted, send me an e-mail if you feel like it and
I'll be happy to help...

  Triso-21                     < There is no answer, only death > WH40K



Sat, 28 Mar 1998 03:00:00 GMT  
 How access alloc'ed string as array?
Quote:

>I would like to allocate a block of memory from the heap and then
>treat it as a two-dimensional array.  I know it's easy, but I just
>can't seem to get the syntax right.  My compiler manual offers no
>help.  Thanks

Could you give a little more info on exactly what you'll do with
this two dimensional array?  Will you continually need to add to it?
Or will you just allocate it once, and use it as is for the rest of
the program?


Sun, 29 Mar 1998 03:00:00 GMT  
 How access alloc'ed string as array?

Quote:


>>>I would like to allocate a block of memory from the heap and then
>>>treat it as a two-dimensional array.  I know it's easy, but I just
>>>can't seem to get the syntax right.  My compiler manual offers no
>>>help.  Thanks

>I think that the following code might be along the lines of what you
>are looking for...

>#include <stdio.h>
>#include <stdlib.h>
>#include <string.h>

>int main()
>{
>    char **array;
>    int i;
>    char string[] = "hi\n";

>    array = (char **) malloc(first_dim_size * sizeof(char *));

There is no need to cast the return of malloc and unnecessary can, in
some cases hide errors.  I guess we're assuming that first_dim_size was
defined and initialized somewhere.

You should test the return of any allocation for failure:

     if( array == NULL)
         {
         fprintf( stderr, "Allocation failure.\n");
         exit( EXIT_FAILURE);
         }

Quote:
>    /* the above creates the space needed for the first dimension */
>    /* of the desired array.                                      */

>    for (i = 0; i < first_dim_size; i++)
>        *array = (char *) malloc(sizeof(char *));

?? I think you intended something more like:

        array[i] = malloc(sizeof(string) + 1);
        /* same allocation test as above on each iteration */

The c.l.c faq discusses several methods of allocating two dimensional
arrays.  You can ftp it from rtfm.mit.edu /pub/usenet/comp.lang.c.

--
John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:



Tue, 31 Mar 1998 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. non-local pointers to c(m)alloc'ed arrays

2. HELP: Dynamically Alloc'd array

3. Using calloc'ed data in threads causes access violation

4. Length of cout'ed string

5. can {m,c}alloc'ed arrays be freed without knowing size?

6. dup()'ed pipe()'s to stdio

7. Dynamic Alloc - Arrays

8. HELP: ways to alloc multidimensional arrays

9. global alloc and numeric arrays

10. (*)alloc for large arrays?

11. Copy Array string to another Pointer array string in ANSI-C

12. Difference between two string alloc

 

 
Powered by phpBB® Forum Software