Thread: Big Problem With Passing an Matrix to Main Function

1. Big Problem With Passing an Matrix to Main Function

Dudes I feel sorry for what I'm going to do now but I'm in real trouble, I've been trying to compile this code for 3 days with no succes.

The code is an implementation of a table of descriptors with double linked lists that receive fields of information according with a hash function (key mod 7). Here follows the code:

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <math.h>

//this struct is the struct that describes an cell of information, 2 numeric and a string //field
typedef struct dataReg{
char nome[80];
long int nascimento;
int numero;
struct dataReg *prox;
struct dataReg *ant;
}datReg;

//here I have the main tab, with links to the first, actual, and last node of data
typedef struct dataTab{
datReg *prim;
datReg *atual;
datReg *ult;
}datTab;

//the table has 7 entries, (something mod 7)+1 = 7 possible values
datTab tabela[7];

int chave;
//initializes the table
int inicializa(tabela[]){
int i = 0;

for(i = 0; i<=7; i++);
tab[i]->prim = tab[i]->atual = tab[i]->ult = NULL;
}
/*inserts in the table, table = tabela, Ive used a switch of 7 elements cause (something mod 7) +1, will allwys generate a value between 0 and 6, and I have a table of 1 to 7, I know matrix starts at 0 it is easy to correct...*/
int insere(tabela[]){
datReg *aux;
int hashmod;

aux = (datReg *)malloc(sizeof(datReg));
printf("\nNome: ");
scanf("%s", aux->nome);
printf("\nData de Nascimento (DDMMAAAA): ");
scanf("%l", aux->nascimento);
printf("\nNumero: ");
scanf("%d", aux->numero);
aux->prox = aux->ant = NULL;
hashmod = int (fmod(aux->numero,7) + 1);
switch(hashmod){
case 1: {
if(tab[1].prim != NULL){
while(tab[1].atual->prox != NULL)
tab[1].atual = tab[1].atual->prox;
tab[1].atual->prox = aux;
aux->ant = tab[1].atual;
tab[1].ult = aux;
}
else{
tab[1].prim = aux;
tab[1].atual = aux;
tab[1].ult = aux;
}
break;
}
case 2: {
if(tab[2].prim != NULL){
while(tab[2].atual->prox != NULL)
tab[2].atual = tab[1].atual->prox;
tab[2].atual->prox = aux;
aux->ant = tab[2].atual;
tab[2].ult = aux;
}
else{
tab[2].prim = aux;
tab[2].atual = aux;
tab[2].ult = aux;
}
break;
}
case 3: {
if(tab[3].prim != NULL){
while(tab[3].atual->prox != NULL)
tab[3].atual = tab[1].atual->prox;
tab[3].atual->prox = aux;
aux->ant = tab[3].atual;
tab[3].ult = aux;
}
else{
tab[3].prim = aux;
tab[3].atual = aux;
tab[3].ult = aux;
}
break;
}
case 4: {
if(tab[4].prim != NULL){
while(tab[4].atual->prox != NULL)
tab[4].atual = tab[4].atual->prox;
tab[4].atual->prox = aux;
aux->ant = tab[4].atual;
tab[4].ult = aux;
}
else{
tab[4].prim = aux;
tab[4].atual = aux;
tab[4].ult = aux;
}
break;
}
case 5: {
if(tab[5].prim != NULL){
while(tab[5].atual->prox != NULL)
tab[5].atual = tab[5].atual->prox;
tab[5].atual->prox = aux;
aux->ant = tab[5].atual;
tab[5].ult = aux;
}
else{
tab[5].prim = aux;
tab[5].atual = aux;
tab[5].ult = aux;
}
break;
}
case 6: {
if(tab[6].prim != NULL){
while(tab[6].atual->prox != NULL)
tab[6].atual = tab[6].atual->prox;
tab[6].atual->prox = aux;
aux->ant = tab[6].atual;
tab[6].ult = aux;
}
else{
tab[6].prim = aux;
tab[6].atual = aux;
tab[6].ult = aux;
}
break;
}
case 7: {
if(tab[7].prim != NULL){
while(tab[7].atual->prox != NULL)
tab[7].atual = tab[7].atual->prox;
tab[7].atual->prox = aux;
aux->ant = tab[7].atual;
tab[7].ult = aux;
}
else{
tab[7].prim = aux;
tab[7].atual = aux;
tab[7].ult = aux;
}
break;
}
}
}
//removes from the table, chave = value to be removed
int remove(tabela[], int chave){
int hashmod;
datReg *aux;

aux = (datReg *)malloc(sizeof(datReg));
aux->numero = 0;

hashmod = int (fmod(chave,7) + 1);
switch(hashmod){
case 1: {
tab[1]->atual = tab[1]->prim;
aux = tab[1]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
aux->ant->prox = aux->prox;
aux->prox->ant = aux->ant;
free(aux);
break;
}
case 2: {
tab[2]->atual = tab[2]->prim;
aux = tab[2]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
aux->ant->prox = aux->prox;
aux->prox->ant = aux->ant;
free(aux);
break;
}
case 3: {
tab[3]->atual = tab[3]->prim;
aux = tab[3]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
aux->ant->prox = aux->prox;
aux->prox->ant = aux->ant;
free(aux);
break;
}
case 4: {
tab[4]->atual = tab[4]->prim;
aux = tab[4]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
aux->ant->prox = aux->prox;
aux->prox->ant = aux->ant;
free(aux);
break;
}
case 5: {
tab[5]->atual = tab[5]->prim;
aux = tab[5]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
aux->ant->prox = aux->prox;
aux->prox->ant = aux->ant;
free(aux);
break;
}
case 6: {
tab[6]->atual = tab[6]->prim;
aux = tab[6]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
aux->ant->prox = aux->prox;
aux->prox->ant = aux->ant;
free(aux);
break;
}
case 7: {
tab[7]->atual = tab[7]->prim;
aux = tab[7]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
aux->ant->prox = aux->prox;
aux->prox->ant = aux->ant;
free(aux);
break;
}
}
}
//searches the table for an item
int consulta(tabela[], int chave){
int hashmod;
datReg *aux;

aux = (datReg *)malloc(sizeof(datReg));
aux->numero = 0;

hashmod = int (fmod(chave,7) + 1);
switch(hashmod){
case 1: {
tab[1]->atual = tab[1]->prim;
aux = tab[1]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
printf("Numero: %d\nNome: %s\nNascimento: %l\n\n", aux->numero, aux->nome, aux->nascimento);
return 1;
}
case 2: {
tab[2]->atual = tab[2]->prim;
aux = tab[2]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
printf("Numero: %d\nNome: %s\nNascimento: %l\n\n", aux->numero, aux->nome, aux->nascimento);
return 1;
}
case 3: {
tab[3]->atual = tab[3]->prim;
aux = tab[3]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
printf("Numero: %d\nNome: %s\nNascimento: %l\n\n", aux->numero, aux->nome, aux->nascimento);
return 1;
}
case 4: {
tab[4]->atual = tab[4]->prim;
aux = tab[4]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
printf("Numero: %d\nNome: %s\nNascimento: %l\n\n", aux->numero, aux->nome, aux->nascimento);
return 1;
}
case 5: {
tab[5]->atual = tab[5]->prim;
aux = tab[5]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
printf("Numero: %d\nNome: %s\nNascimento: %l\n\n", aux->numero, aux->nome, aux->nascimento);
return 1;
}
case 6: {
tab[6]->atual = tab[6]->prim;
aux = tab[6]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
printf("Numero: %d\nNome: %s\nNascimento: %l\n\n", aux->numero, aux->nome, aux->nascimento);
return 1;
}
case 7: {
tab[7]->atual = tab[7]->prim;
aux = tab[7]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
printf("Numero: %d\nNome: %s\nNascimento: %l\n\n", aux->numero, aux->nome, aux->nascimento);
return 1;
}
}
}

int main(){
int opcao;

while(opcao != 4){
printf("\n\n\n\nOpcoes:\n1: Consultar tabela.\n2: Inserir.\n3: Excluir.\n4: Sair.");
scanf("%c", &opcao);
switch(opcao){
case 1: {
int num;

printf("\nNumero: ");
scanf("%d", &num);
//                num = int (fmod(num,7) + 1);
consulta(tabela[],num);
break;
}
case 2: {
insere(tabela[]);
}
case 3: {
int num;

printf("\nNumero: ");
scanf("%d", &num);
remove(tabela[]);
}
case 4: return 0;
default: printf("Escolha outra opcao!!!\n\n\n");
}
}
}```
Sorry again, if someone can read portuguese my hope lies on you. Ohhhhhh the main doubt = how can I pass tabela to the functions, I'm 99% sure the problem lies there, sorry again and thx again.

2. Code:
`int inicializa(tabela[]){`
should be
Code:
`int inicializa(datTab tabela[])`
tab is undeclared I think it should be a dataTab. You have alot of errors. It isn't near being able to compile. Break your program down into smaller parts then find out what is wrong.

3. Thx a lot this is the kind of help I'm needing Well I solved very minor bugs I've just found it is 1:24 AM here I must sleep! But I will re-submit the code, if you notice something else like this big errors please notice me!

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <math.h>

typedef struct dataReg{
char nome[80];
long int nascimento;
int numero;
struct dataReg *prox;
struct dataReg *ant;
}datReg;

typedef struct dataTab{
datReg *prim;
datReg *atual;
datReg *ult;
}datTab;

datTab tabela[7];

int chave;

int inicializa(dataTab tabela[]){
int i = 0;

for(i = 0; i<=7; i++);
tab[i]->prim = tab[i]->atual = tab[i]->ult = NULL;
}

int insere(dataTab tabela[]){
datReg *aux;
int hashmod;

aux = (datReg *)malloc(sizeof(datReg));
printf("\nNome: ");
scanf("%s", aux->nome);
printf("\nData de Nascimento (DDMMAAAA): ");
scanf("%l", aux->nascimento);
printf("\nNumero: ");
scanf("%d", aux->numero);
aux->prox = aux->ant = NULL;
hashmod = int (fmod(aux->numero,7) + 1);
switch(hashmod){
case 1: {
if(tab[1].prim != NULL){
while(tab[1].atual->prox != NULL)
tab[1].atual = tab[1].atual->prox;
tab[1].atual->prox = aux;
aux->ant = tab[1].atual;
tab[1].ult = aux;
}
else{
tab[1].prim = aux;
tab[1].atual = aux;
tab[1].ult = aux;
}
break;
}
case 2: {
if(tab[2].prim != NULL){
while(tab[2].atual->prox != NULL)
tab[2].atual = tab[1].atual->prox;
tab[2].atual->prox = aux;
aux->ant = tab[2].atual;
tab[2].ult = aux;
}
else{
tab[2].prim = aux;
tab[2].atual = aux;
tab[2].ult = aux;
}
break;
}
case 3: {
if(tab[3].prim != NULL){
while(tab[3].atual->prox != NULL)
tab[3].atual = tab[1].atual->prox;
tab[3].atual->prox = aux;
aux->ant = tab[3].atual;
tab[3].ult = aux;
}
else{
tab[3].prim = aux;
tab[3].atual = aux;
tab[3].ult = aux;
}
break;
}
case 4: {
if(tab[4].prim != NULL){
while(tab[4].atual->prox != NULL)
tab[4].atual = tab[4].atual->prox;
tab[4].atual->prox = aux;
aux->ant = tab[4].atual;
tab[4].ult = aux;
}
else{
tab[4].prim = aux;
tab[4].atual = aux;
tab[4].ult = aux;
}
break;
}
case 5: {
if(tab[5].prim != NULL){
while(tab[5].atual->prox != NULL)
tab[5].atual = tab[5].atual->prox;
tab[5].atual->prox = aux;
aux->ant = tab[5].atual;
tab[5].ult = aux;
}
else{
tab[5].prim = aux;
tab[5].atual = aux;
tab[5].ult = aux;
}
break;
}
case 6: {
if(tab[6].prim != NULL){
while(tab[6].atual->prox != NULL)
tab[6].atual = tab[6].atual->prox;
tab[6].atual->prox = aux;
aux->ant = tab[6].atual;
tab[6].ult = aux;
}
else{
tab[6].prim = aux;
tab[6].atual = aux;
tab[6].ult = aux;
}
break;
}
case 7: {
if(tab[7].prim != NULL){
while(tab[7].atual->prox != NULL)
tab[7].atual = tab[7].atual->prox;
tab[7].atual->prox = aux;
aux->ant = tab[7].atual;
tab[7].ult = aux;
}
else{
tab[7].prim = aux;
tab[7].atual = aux;
tab[7].ult = aux;
}
break;
}
}
}

int remove(dataTab tabela[], int chave){
int hashmod;
datReg *aux;

aux = (datReg *)malloc(sizeof(datReg));
aux->numero = 0;

hashmod = int (fmod(chave,7) + 1);
switch(hashmod){
case 1: {
tab[1]->atual = tab[1]->prim;
aux = tab[1]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
aux->ant->prox = aux->prox;
aux->prox->ant = aux->ant;
free(aux);
break;
}
case 2: {
tab[2]->atual = tab[2]->prim;
aux = tab[2]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
aux->ant->prox = aux->prox;
aux->prox->ant = aux->ant;
free(aux);
break;
}
case 3: {
tab[3]->atual = tab[3]->prim;
aux = tab[3]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
aux->ant->prox = aux->prox;
aux->prox->ant = aux->ant;
free(aux);
break;
}
case 4: {
tab[4]->atual = tab[4]->prim;
aux = tab[4]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
aux->ant->prox = aux->prox;
aux->prox->ant = aux->ant;
free(aux);
break;
}
case 5: {
tab[5]->atual = tab[5]->prim;
aux = tab[5]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
aux->ant->prox = aux->prox;
aux->prox->ant = aux->ant;
free(aux);
break;
}
case 6: {
tab[6]->atual = tab[6]->prim;
aux = tab[6]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
aux->ant->prox = aux->prox;
aux->prox->ant = aux->ant;
free(aux);
break;
}
case 7: {
tab[7]->atual = tab[7]->prim;
aux = tab[7]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
aux->ant->prox = aux->prox;
aux->prox->ant = aux->ant;
free(aux);
break;
}
}
}

int consulta(dataTab tabela[], int chave){
int hashmod;
datReg *aux;

aux = (datReg *)malloc(sizeof(datReg));
aux->numero = 0;

hashmod = int (fmod(chave,7) + 1);
switch(hashmod){
case 1: {
tab[1]->atual = tab[1]->prim;
aux = tab[1]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
printf("Numero: %d\nNome: %s\nNascimento: %l\n\n", aux->numero, aux->nome, aux->nascimento);
return 1;
}
case 2: {
tab[2]->atual = tab[2]->prim;
aux = tab[2]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
printf("Numero: %d\nNome: %s\nNascimento: %l\n\n", aux->numero, aux->nome, aux->nascimento);
return 1;
}
case 3: {
tab[3]->atual = tab[3]->prim;
aux = tab[3]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
printf("Numero: %d\nNome: %s\nNascimento: %l\n\n", aux->numero, aux->nome, aux->nascimento);
return 1;
}
case 4: {
tab[4]->atual = tab[4]->prim;
aux = tab[4]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
printf("Numero: %d\nNome: %s\nNascimento: %l\n\n", aux->numero, aux->nome, aux->nascimento);
return 1;
}
case 5: {
tab[5]->atual = tab[5]->prim;
aux = tab[5]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
printf("Numero: %d\nNome: %s\nNascimento: %l\n\n", aux->numero, aux->nome, aux->nascimento);
return 1;
}
case 6: {
tab[6]->atual = tab[6]->prim;
aux = tab[6]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
printf("Numero: %d\nNome: %s\nNascimento: %l\n\n", aux->numero, aux->nome, aux->nascimento);
return 1;
}
case 7: {
tab[7]->atual = tab[7]->prim;
aux = tab[7]->atual;
while(aux->numero != chave){
if(aux->prox == NULL){
return 0;
}
else
aux = aux->prox;
}
printf("Numero: %d\nNome: %s\nNascimento: %l\n\n", aux->numero, aux->nome, aux->nascimento);
return 1;
}
}
}

int main(){
int opcao;

while(opcao != 4){
printf("\n\n\n\nOpcoes:\n1: Consultar tabela.\n2: Inserir.\n3: Excluir.\n4: Sair.");
scanf("%c", &opcao);
switch(opcao){
case 1: {
int num;

printf("\nNumero: ");
scanf("%d", &num);
//                num = int (fmod(num,7) + 1);
consulta(dataTab *tabela[],int num);
break;
}
case 2: {
insere(dataTab *tabela[]);
}
case 3: {
int num;

printf("\nNumero: ");
scanf("%d", &num);
remove(dataTab *tabela[]);
}
case 4: return 0;
default: printf("Escolha outra opcao!!!\n\n\n");
}
}
}```

4. Code:
```>int inicializa(dataTab tabela[]){
>    int i = 0;
>
>    for(i = 0; i<=7; i++);
>        tab[i]->prim = tab[i]->atual = tab[i]->ult = NULL;
>}```
The name you are passing is tabela, but then you use the name tab. Change one or the other. Also:
>datTab tabela[7];
This array runs from 0 to 6, but your for-loop above goes from 0 to 7. Change it to:
for(i = 0; i<7; i++);

You have this problem in several places.

5. > tab[i]->prim = tab[i]->atual = tab[i]->ult = NULL;

Also, you are passing an array, not a pointer, so I think you should use ., not ->.
tab[i].prim = tab[i].atual = tab[i].ult = NULL;

But I could be wrong.