Problem Understanding this

• 06-09-2010
RA-3000
Problem Understanding this
Hi everyone,

I've been working on a program that I need to do for Numerical Analyses. I use memory allocation with a 2D array.

I'm not really used to this, but my teacher says that the program should not work because I'm not using the 2D array correctly. He tried to explain to me why it should not work, but it works.

I've learned to use this on a website, but my teacher wants me to explain how my program works. The problem is that I'm passing the 2D array to a function (I pass the memory location to that function) and it receives just like this:

void resolve_sistema(int eq, float matriz[][eq+1])

He says that It should not work like this. For example, when I want to use the 2D array I use it like this: matriz[5][5], but my teacher says it should be done like this: *(*(matriz+5)+5).

Can someone Help?
• 06-09-2010
Bayint Naung
Post the relevant code.
How you allocate 2-D array.
Pass to function. how it is used in function.

matrix[5][5] and *(*(matriz+5)+5) are the same.
• 06-09-2010
RA-3000
He says to me that I cannot use matriz[5][5] because that doesn't exist.

BTW, the allocation:

Code:

```float **criacao_tdd (int pola, int *c) //tdd[c][c+1] c= numero de pontos {     float **tdd;     int  n;     char op;     do{         system("cls");         printf("\t\t\tCriacao da Tabela Das Diferencas Divididas \n\nQuantos pontos se utilizara na tabela? \n(Numero de pontos máximos: %d)", pola);         scanf("%d", c);         if(*c>1 && *c <= pola)             do{                 printf("\nDeseja Alterar o valor introduzido? S/N");                 op = toupper(getch());             }while(op != 'S' && op != 'N');     }while(*c<=1 || *c>pola || op == 'S');     tdd = (float **) calloc (*c, sizeof(float *));     for ( n = 0; n < *c; n++ )     {         tdd[n] = (float*) calloc (*c+1, sizeof(float));         if (tdd[n] == NULL)             break;     }     if(tdd == NULL || tdd[n] == NULL)     {         do{             printf("\nErro: não há memória \nDeseja introduzir um novo valor? (S/N) \n\n");             op = toupper(getch());         }while(op != 'S' && op != 'N');         if (op == 'N')             exit(1);         else             tdd=criacao_tdd(pola, c);     }     return (tdd); }```

EDIT: When I call the function that allocates this, I do this: tdd = criacao_tdd(pola, c);

One function that use it:

Code:

```int px (int c, float tdd[][c+1], float x) {     int coluna = 1, linha = 0, op_erro, aux = 0;     float px = 0, aux_calculo;     char op;     do{         system("cls");         printf("Deseja Calcular o Erro? ");         op = toupper(getch());         if (op == 'S')             op_erro = 1;         if (op == 'N')             op_erro = 0;     }while(op_erro != 1 && op_erro != 0);     for(coluna = 1; coluna < c+1-op_erro; coluna++)     {         aux_calculo = tdd[0][coluna];         for(linha = 0; linha < aux; linha++)             aux_calculo = aux_calculo*(x-tdd[linha][0]);         aux++;         px = px+aux_calculo;     }     printf("\n\nX = %f ",px);     return op_erro; }```
• 06-09-2010
Bayint Naung
Where is matriz[5][5]?
Why not english ? :(
• 06-09-2010
RA-3000
Sorry for the portuguese. There is no matriz[5][5]. That was an example. There is tdd[][].

When I use this:
aux_calculo = tdd[0][coluna];

My teacher says it should not work and that I have to use this:
aux_calculo = *(*(tdd+0)+coluna);
• 06-09-2010
Bayint Naung
How do you call px() function.
especially 2 D passing.
• 06-09-2010
RA-3000
I call it like this (in the main function):
op_erro = px(colunas, tdd, x);
• 06-09-2010
Elysia
Then you can merilly go tell your teacher that he/she is wrong.
Both of these syntaxes are valid:
aux_calculo = tdd[0][coluna];
aux_calculo = *(*(tdd+0)+coluna);
The former is better because it's more readable.
• 06-09-2010
Bayint Naung
Quote:

He tried to explain to me why it should not work, but it works.
I wonder how he explain?
• 06-13-2010
RA-3000
Thanks everyone. He keeps telling me that it should not work and I am wrong -_-