Sign in to follow this  
Dogh Quch

Problems with Dynamic Matrix....

Recommended Posts

Hello people, I'm having a really hard time trying to use a dynamic matrix in my program. I never had to use something like this and when I tried it never worked...but this time is the only way! So, this is how I'm doing it, I have the declaration of the matrix like this: int **mi_Bord; And I’m making a malloc like this: MyIsoMap->mi_Bord=(int **) MALLOC(((MSize/2)+1)*sizeof(int *)); for (i=(MSize/2)+1; --i; ) MyIsoMap->mi_Bord[i]=(int *)MALLOC(((MSize/2)+1)*sizeof (int)); They are in capital letters because this is Brew code and not C standard. The problem is, when I try to make an value attribution there is an error. I know the way for doing an attribution is like this: **iso->IsoTeste.mi_Bord[j][i]=1; But the compiler only let me do like this: iso->IsoTeste.mi_Bord[j][i]=1; And that is why it happens an error! Can some one help me about the correct way of doing this?

Share this post


Link to post
Share on other sites
hi,

first let me say that I don't know Brew, so I'll just have to tell you the things I know hold for C; hope they apply as well.

Your memory allocation seems to be in order (although right now, I am really not sure, if it isn't supposed to be sizeof(int) as well in the first call---try that for a change, but it's a wild guess), but what I do not get is the way you call your array.
Usually it's like this:

int **matrix;

matrix = (int **) malloc (columns * sizeof(int)); // maybe sizeof(int *) -- can't remember.

for (int i = 0; i < columns; i++) {
matrix[i] = (int *) malloc(rows * sizeof(int));
}

and then you should be able to access it via:

matrix[x][y] = 19;

Maybe not much of a help... But you can give it a try, maybe it'll help...

BTW: Maybe you have the chance to switch to C++?! It's easier there...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this