Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

malloc in C, but use multi-dimensional array syntax

Is there any way to malloc a large array, but refer to it with 2D syntax? I want something like:

int *memory = (int *)malloc(sizeof(int)*400*200);
int MAGICVAR = ...;
MAGICVAR[20][10] = 3; //sets the (200*20 + 10)th element


UPDATE: This was important to mention: I just want to have one contiguous block of memory. I just don't want to write a macro like:
#define INDX(a,b) (a*200+b);

and then refer to my blob like:

memory[INDX(a,b)];

I'd much prefer:

memory[a][b];


UPDATE: I understand the compiler has no way of knowing as-is. I'd be willing to supply extra information, something like:
int *MAGICVAR[][200] = memory;

Does no syntax like this exist? Note the reason I don't just use a fixed width array is that it is too big to place on the stack.


UPDATE: OK guys, I can do this:
void toldyou(char MAGICVAR[][286][5]) {
  //use MAGICVAR
}

//from another function:
  char *memory = (char *)malloc(sizeof(char)*1820*286*5);
  fool(memory);

I get a warning, passing arg 1 of toldyou from incompatible pointer type, but the code works, and I've verified that the same locations are accessed. Is there any way to do this without using another function?

like image 633
Claudiu Avatar asked Jun 29 '10 19:06

Claudiu


People also ask

What is the syntax for multidimensional array in C?

The basic form of declaring a two-dimensional array of size x, y: Syntax: data_type array_name[x][y]; Here, data_type is the type of data to be stored.

What is the correct syntax to declare a multidimensional array?

Data in multidimensional arrays are stored in tabular form (in row major order). Syntax: data_type[1st dimension][2nd dimension][].. [Nth dimension] array_name = new data_type[size1][size2]….

Can you malloc a 2D array in C?

A 2D array can be dynamically allocated in C using a single pointer. This means that a memory block of size row*column*dataTypeSize is allocated using malloc and pointer arithmetic can be used to access the matrix elements.

Can we use multidimensional array in C?

In C programming, you can create an array of arrays. These arrays are known as multidimensional arrays. For example, float x[3][4];


2 Answers

Yes, you can do this, and no, you don't need another array of pointers like most of the other answers are telling you. The invocation you want is just:

int (*MAGICVAR)[200] = malloc(400 * sizeof *MAGICVAR);
MAGICVAR[20][10] = 3; // sets the (200*20 + 10)th element

If you wish to declare a function returning such a pointer, you can either do it like this:

int (*func(void))[200]
{
    int (*MAGICVAR)[200] = malloc(400 * sizeof *MAGICVAR);
    MAGICVAR[20][10] = 3;

    return MAGICVAR;
}

Or use a typedef, which makes it a bit clearer:

typedef int (*arrayptr)[200];

arrayptr function(void)
{
    /* ... */
like image 75
caf Avatar answered Oct 24 '22 20:10

caf


Use a pointer to arrays:

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

int main()
{
    int (*arr)[10];

    arr = malloc(10*10*sizeof(int));
    for (int i = 0; i < 10; i++)
        for(int j = 0; j < 10; j++)
            arr[i][j] = i*j;

    for (int i = 0; i < 10; i++)
        for(int j = 0; j < 10; j++)
            printf("%d\n", arr[i][j]);
    free(arr);
    return 0;
}
like image 32
Tim Schaeffer Avatar answered Oct 24 '22 19:10

Tim Schaeffer