-1

I'm trying to create a 2D matrix of ints and allocate memory to the same using malloc().
I want the matrix to look like this: {{-4,0},{-3,0},{-2,0},{-1,0},{1,0},{2,0},{3,0},{4,0}} But I want to be able to change it later, so I'm trying to dynamically allocate a contiguous block using malloc(). I've created :

typedef int** scores_table

so my function can return the type scores_table.

This is my code:

scores_table alloc_scores_table(){
    scores_table scores;
    int i,j, *row;
    row=(int*)malloc(sizeof(int)*2*8);
    scores=(scores_table)malloc(sizeof(int*)*8);
    if(scores==NULL || row==NULL){
        quit();
    }
    for(i=0;i<8;i++){
        scores[i]=row+i*2;
    }
    for(i=0;i<8;i++){
        for(j=0;j<2;j++){
            if(j==1){
                scores[i][j]=0;
            }
            else{
                if(i>3){
                    scores[i][j]=-4+1+i;
                }
                else{
                    scores[i][j]=-4+i;
                }
            }
        }
    }
    return scores;
}

The problem is - the function is returning only -4 and I have no idea why. What am I doing wrong?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • You're doing too much in one function thus making it overly complex. Create a `struct` for your data, implement a getter and setter for (x,y) and use them. – Dariusz Jan 03 '14 at 08:58
  • 1
    could you write the display function you use too ? – Pierre Emmanuel Lallemant Jan 03 '14 at 09:03
  • 1
    and, don't cast the return value of `malloc()`. – Sourav Ghosh Jan 03 '14 at 09:04
  • How you are checking the return value of the function? –  Jan 03 '14 at 09:13
  • Add to the list of tips: `-4+1` could be written as `-3`. – Jite Jan 03 '14 at 09:19
  • I haven't checked the display of the return function, I'm trying to pass it to another function and change the number in socres[i][j], but it seems that the function is not getting the entire array when i try to pass scores to it (scores_table scores is the parameter of the function) – user3156437 Jan 03 '14 at 10:08
  • @user3156437: It's a bit hard to guess what you are doing wrong when you pass the array to that other function, when _you don't show how you are doing it!_ That's why it's so important to provide a [Short, Self Contained, Correct (Compilable), Example!](http://sscce.org/) – Thomas Padron-McCarthy Jan 03 '14 at 11:09
  • @user3156437: However, if I really am to guess what's wrong, perhaps that other function doesn't take a pointer to pointer as its argument, but instead a matrix, for example with a declaration such as **void otherfunction(int argument[8][2])**? It happens sometimes that people misunderstand how arrays, especially two-dimensional arrays, work in C, and think that there are more pointers involved than there really is. – Thomas Padron-McCarthy Jan 05 '14 at 10:33

2 Answers2

1

Why not have the array itself malloc'd?

typedef int scores_table[2][8];

scores_table* alloc_scores_table()
{
    scores_table *scores = malloc(sizeof *scores);
    if(scores)
    {
        // your initalization code here
        size_t i, j;
        for(i = 0; i < 8; i++) {
            for(j = 0; j < 2; j++) {
                if(j == 1) {
                    *scores[i][j]=0;
                }
                else {
                    if(i > 3) {
                        *scores[i][j]=-4+1+i;
                    }
                    else {
                        *scores[i][j]=-4+i;
                    }
                }
            }
        }
    }

    return scores;
}
legends2k
  • 31,634
  • 25
  • 118
  • 222
1

You are probably doing something wrong when you print the result. This code:

int main(void) {
    scores_table st = alloc_scores_table();

    for (int i = 0; i < 8; ++i) {
        for (int j = 0; j < 2; ++j) {
            printf("st[%d][%d] = %d\n", i, j, st[i][j]);
        }
    }

    return 0;
}

gives this output:

st[0][0] = -4
st[0][1] = 0
st[1][0] = -3
st[1][1] = 0
st[2][0] = -2
st[2][1] = 0
st[3][0] = -1
st[3][1] = 0
st[4][0] = 1
st[4][1] = 0
st[5][0] = 2
st[5][1] = 0
st[6][0] = 3
st[6][1] = 0
st[7][0] = 4
st[7][1] = 0

which I think is what you expected?

Thomas Padron-McCarthy
  • 27,232
  • 8
  • 51
  • 75
  • I'm not trying to print the result, I'm trying to pass the scores table to another function so that I can change the numbers, for example do: st[0][1]=1 but it is not passing the entire array. How can I pass the array to another function? – user3156437 Jan 03 '14 at 09:51
  • @user3156437: It's a bit hard to guess what you are doing wrong when you pass the array to that other function, when _you don't show how you are doing it!_ That's why it's so important to provide a [Short, Self Contained, Correct (Compilable), Example!](http://sscce.org/) – Thomas Padron-McCarthy Jan 03 '14 at 11:07