Thread: making an empty 2-D array

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    202

    making an empty 2-D array

    Hi everyone,

    I've got this problem where I have a subroutine finish and return a 2 dimensional array of a non-predetermined size. My problem is getting this same array into another c program. Right now I have something to the effect of

    char *ptr;

    /* Other code */

    ptr = call_arv("qsheet", G_NOARGS, args);

    where qsheet is the name of a perl subroutine, G_NOARGS tells the subroutine to take no arguments, and args is an empty argument list.

    Obviously, ptr isn't getting the 2D array.

    Can anyone tell me how to make it so that it will?

    Thanks,

    starX
    www.axisoftime.com

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You can do

    char *ptr;

    and use malloc to create an 1D array of size

    NR_OF_COLUMS x NR_OF_ROWS

    and then calculate the positions when needed.

    So the matrix

    [1 2 3]
    [4 5 6]
    [7 8 9]

    will be stored as

    [1 2 3 4 5 6 7 8 9]

    And you can reach for example 5 by

    row = 1;
    colum = 1;
    val = ptr [row * 3 + colum];

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Or something like this. I'm not sure about the allocation of memory for ptr. Though it seems to work well.

    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    
    #define COLS 2
    #define ROWS 3
    
    char **create_2d_array (int colums, int rows)
    {
        char **ptr;
        int i;
        
        for (i = 0; i < rows; i++)
        {
            ptr [i] = (char *) malloc (colums);
        }
    
        return ptr;
    }
    
    int main(int argc, char **argv) 
    { 
        char **array_2d;
        int i, j;
        
        array_2d = create_2d_array (COLS, ROWS);
    
        for (i = 0; i < ROWS; i++)
        {
            for (j = 0; j < COLS; j++)
            {
                array_2d [i][j] = 1;
            }
        }
    
        for (i = 0; i < ROWS; i++)
        {
            for (j = 0; j < COLS; j++)
            {
                array_2d [i][j] = 1;
                printf ("%d ", array_2d [i][j]);
            }
            printf ("\n");
        }
        
        
        for (i = 0; i < ROWS; i++)
        {
            free (array_2d [i]);
        }
        
        return 0;    
    }

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I don't suppose you could put up some information on this call_arv function?
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    202
    call_argv, sorry. It's one of the functions related to calling the perl subroutine from c.

    In my most recent reading, I've discovered that the function itself doesn't return directly to the c function, but I've figured out a way to get it. I still am working on how to have the array of an undertimed size.

    Thanks Shiro, I'll give that a shot sometime tomorrow... I've really got to get some sleep now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Empty Array?
    By Terran in forum C++ Programming
    Replies: 2
    Last Post: 06-16-2008, 12:47 AM
  2. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  3. problem with making characters in an array different colors
    By o0obruceleeo0o in forum C++ Programming
    Replies: 0
    Last Post: 04-27-2003, 12:13 PM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. making an array name a variable
    By Zaarin in forum C++ Programming
    Replies: 5
    Last Post: 09-02-2001, 06:17 AM