Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Creating n amount of arrays in C?

Name: Anonymous 2013-03-07 21:18

A simple question, i hope there's a simple answer.

The program I'm making will take in data, and based on an integer, I'd like to make a number of arrays based on that integer.

Any help? Thanks.

Name: Anonymous 2013-03-08 0:52

>>9
Muh heap fragmentation

int **alloc2d(size_t m, size_t n)
{
        size_t vectorsz, rowsz;
        int **vector;
        int *row;
        int i;

        vectorsz = m * sizeof(int *);
        rowsz = n * sizeof(int);

        if (!(vector = malloc((vectorsz + m * rowsz))))
                return NULL;

        row = (int *) (vector + m);
        for (i = 0; i < m; ++i) {
                vector[i] = row;
                row += n;
        }

        return vector;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List