Thread: Help creating a 2D array

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    13

    Help creating a 2D array

    Hello,

    I am working on a problem statement which typedefs an array like

    Code:
    typedef signed char Type[6]
    the task is to create a function that will take input parameters as rows and cols and create a 2-dimensional pointer array of data type 'Type' having the number of rows and cols passed in as inputs. There is an additional requirement that all memory allocations should happen in only one call to malloc()

    I tried this but don't think it is correct.

    Can someone provide some pointers?

    Code:
    Type **testArray(size_t rows, size_t cols)
    {
        Type **temp = NULL;
    
        temp = (Type **)malloc(rows * cols * sizeof(Type));
        if (temp == NULL)
        {
            //error out
        }
        
        return temp;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,541
    The amount of memory you need to allocate is
    rows * sizeof(Type*) + rows * cols * sizeof(Type)

    The first part will be your array of Type* pointers, the beginning of which is your actual return value.

    You then need to initialise 'rows' of pointers to point to 'cols' sized blocks of Type in the second part of the memory block.

    Also, this is C, so there is no need to cast the return result of malloc (see the FAQ).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    358
    2D arrays don't work well in C when the dimensions are not known at compile time.

    So what most C programmers will do is to allocate a large 1D array, then access with

    Code:
          array[y*width+x] = value;
    but it's also possible to create an array of pointers

    Code:
            Type **array = malloc(height * sizeof(Type *));
            for(i=0;i<height;i++)
               array[i] = malloc(width * sizeof(Type));
            array[y][x] = value;
    The access syntax is the same as for a built-in 2D array, but the underlying machine operations are different.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating an Array of Labels
    By Mcdom34 in forum C# Programming
    Replies: 4
    Last Post: 10-12-2012, 03:38 PM
  2. Help Creating a 2D array
    By valathena in forum C Programming
    Replies: 3
    Last Post: 03-25-2011, 09:31 PM
  3. Need help creating an array
    By BigWigly in forum C Programming
    Replies: 7
    Last Post: 03-11-2011, 08:29 AM
  4. Creating an array of Strings
    By Vegenad in forum C Programming
    Replies: 5
    Last Post: 03-19-2010, 06:05 AM
  5. creating array
    By brown in forum C Programming
    Replies: 1
    Last Post: 04-08-2009, 04:18 PM

Tags for this Thread