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-07 21:33

#define ARRAY_LENGTH 20

int **array_of_arrays(int n) {
int i;
int **array_of_arrays = malloc(sizeof(int *) * n);
for(i = 0; i < n; i++) {
array_of_arrays[i] = malloc(sizeof(int) * ARRAY_LENGTH);
}
return array_of_arrays;
}

Name: Anonymous 2013-03-07 21:42

>>8
realloc, genius.

Name: Anonymous 2013-03-07 21:44

>>10
don't bother arguing with anyone who uses emoticons, because that "anyone" is always retard-kun and his asstarded lsd trips

Name: Anonymous 2013-03-07 21:53

retard-kun and his asstarded lsd trips

"retard-kun" is a noun and so is "asstarded lsd trips"

But retard-kun is a person too, he can post on /prog/. But asstarded lsd trips can't post, they aren't persons; they aren't even physical! Nor are they Ai or bots. asstarded lsd trips can't do anything, they are just something a person feels!

Name: Anonymous 2013-03-07 21:54

#define nigg_arr int* /* ??? is typedef a part of C proper? I always just compile as C++ ??? */
#define runloop 1 /* set to zero to disable the loop */ /* Useful for debugging */
nigg_arr niggers;  /* new nigger pointer */
int dickSizeInInches=sizeof(nigg_arr); /* will vary among machines, but should be compiled to constant */
int currentNiggerNumber=0; /* the niggers we will work on first */
niggers = (nigg_arr)malloc(numberOfNiggerDicks*dickSizeInInches); /* make a list of niggers */
do{ /* loop so that we don't test test condition first, saving time on a damn cmp instruction */
/* pointer to addr of this nigger */ *(niggers+currentNiggerNumber)=(nigg_arr)malloc(9*dickSizeInInches); /* make a list from each element in the list */
    switch(currentNiggerNumber){ /* logic to decide if we are done */
        numberOfNiggerDicks: /* yup, done */
/* considered harmful */ goto ALL_NIGGERS_TAGGED; /* now we can go */
        default: /*  not done */
/* could also be in ++ notation */ currentNiggerNumber=currentNiggerNumber+1; /* put the next nigger on the operating table :) */
    } /* closes the switch loop */
}/*  closes the while loop */
/* this is ternary notation BTW */ while(1==((runloop==1)?1:0)); /* test conditions for the main program loop */
ALL_NIGGERS_TAGGED: /* label for where we need to go when we finish, in this case, outside of the loop */
exit(0); /* we're done here guys, let's go home! */

Name: Anonymous 2013-03-07 22:05

well, c isn't my forte (that array raping which you suggested...) but i'm not retard kun
i like ^^ emoticon too though (so cute) but i don't use it to avoid being confused with him

Name: Anonymous 2013-03-07 22:11

>>14
intelligence isn't your forté either

Name: Anonymous 2013-03-07 22:35

>>15
mew mew

Name: Anonymous 2013-03-07 23:05

C=
>>11 and you'll lose xD

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;
}

Name: Anonymous 2013-03-08 1:08

check 'em

Name: Anonymous 2013-03-08 2:00

>>18
It annoys me how you think your solution better than >>9, enough to give poor >>9-san the insulting ``muh  heap fragmentation'' when the solution presented in >>18, while somewhat clever, is just as deserving of insult, as it uses a trick to solve a problem that doesn't need to be solved. In other words, the problem is man made, and created by the author of >>18. So commonly do I see this pattern of chastising the efforts of others in comparison to your own imperfect work that I see on /g/, and it makes me think that you frequent this other board. But I cannot know for certain.

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2013-03-08 3:39

>>8
This should be valid C99
int myfunc(int x, int y, int arr[x][y]) {
 int k = 0;
 for(int i=0;i<x;i++)
  for(int j=0;j<y;j++)
   k += arr[i][j];
}

Name: Anonymous 2013-03-08 3:52

oh.. yes, you can do stuff like

int x, y;
scanf("%d", &x);
scanf("%d", &y);
int arr[x][y];
for(int i=0;i<x;i++)
    for(int j=0;j<y;j++)
        arr[i][j] = 1;

Name: Anonymous 2013-03-08 3:54

malloc (sx * sy * sizeof(datatype));
Access with e.g. arr[(y*sx)+x]
Memory is not 2D so (especially in C) you shouldn't pretend it is.

Name: Anonymous 2013-03-08 8:03

>>18
Dude OP is probably a student. If he actually gave a shit about heap fragmentation, he'd already know how to solve this problem and wouldn't be asking us. Keep it simple for the kid.

Name: Anonymous 2013-03-08 9:16

>>14,17
so you're saying your retardation is bad enough to make it difficult to recognize yourself?

holy fuck

Name: Anonymous 2013-03-08 9:38

>>18
You should use a union for that. Or better yet, just make two calls to malloc.

Name: Anonymous 2013-03-08 11:32

>>20,24
>>18 is too clever by half - resizing that Iliffe vector will be painful, and if you really cared that much about performance you'd probably limit the dimensions to a power of 2 anyway. This is /prog/; did you really think I was trying to help him?

Name: Anonymous 2013-03-08 11:37

>>20
Please elaborate on why the problem doesn't need to be solved.

Name: Anonymous 2013-03-08 13:43

>>20
>>9 sucks because it makes n + 1 calls to malloc. That means you'll probably need to make n + 1 calls to free as well, if you're writing a large program and you don't want it to leak like shit. Those calls are all fairly expensive, and making fewer calls to malloc that request a larger size is usually faster and it results in more compact code.

>>18 sucks too though, because it doesn't take alignment requirements into account. Although, that can be fixed with >>26.

Name: Anonymous 2013-03-08 14:54

Please respond to my implemetation in >>13 , you guys are making me feel lonely ;___;.

Name: Anonymous 2013-03-08 15:26

>>30
too spaghetti code; didn't read

Name: Anonymous 2013-03-08 16:10

>>30
I find that everything great I do always goes unresponded on /frot/. Take it as a complement.

Name: Anonymous 2013-03-08 16:57

>>25
it's two different people

Name: Anonymous 2013-03-09 0:56

>>32

Does this mean we only respond to criticize? How sad.

Name: Anonymous 2013-03-09 4:21

>>33 retard dubs! xD

Name: Anonymous 2013-03-09 4:26

no offence intended though, it just means your smarter than the rest xDD

Name: Anonymous 2013-03-09 4:53

Get the gentoomen and read up on data structures. Data structures are the most important, really. Read up on this shit, you will benifit.

Name: Anonymous 2013-03-09 5:08

>>37

your mom is my data structure

Name: Anonymous 2013-03-09 5:58

>>37
use array for everything
da da da da da da da

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