Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Matheus Weber

Problema ao Passar Vetor

Recommended Posts

Não to conseguindo passar um vetor por função, ele dar este erro de compilação:

 

hash.c: In function ‘main’:
hash.c:44:2: warning: passing argument 2 of ‘imprimeVetor’ from incompatible pointer type [enabled by default]
In file included from hash.c:1:0:
hash.h:13:6: note: expected ‘struct Chave *’ but argument is of type ‘struct Chave (*)[(sizetype)(posicoes)]’
funcoes.c: In function ‘imprimeVetor’:
funcoes.c:22:29: error: subscripted value is neither array nor pointer nor vector

 

Eis o código:

 

hash.c

#include "hash.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char** argv) {
	int posicoes = 100;
	Chave vet1[posicoes];
	int numeroElementos = 0;
	int fatorCarga = 0/100;
	
	int i = 0;
	char aux[150], *aux1;
	int aux2;
	FILE *fp;
	
	
	
	if((fp = fopen( "chaves2M.txt", "r")) == NULL ) {
		printf("Arquivo não encontrado\n");
		exit(1);
	}
	
	Chave novo;
	novo.numero = 1;
	novo.string = "lala";
	printf("%d %s", novo.numero, novo.string);
	
	
	while(!feof(fp) && i<10){
		
		fgets(aux,150, fp);
		aux1 = strtok(aux, " ");
		aux2 = atoi(strtok(NULL, " "));
		novo.numero = aux2;	
		novo.string = (char*) malloc (150*sizeof(char));
		strcpy(novo.string,aux1);	
		novo.prox = NULL;
		vet1[hashModelo(aux1, posicoes)] = novo;
		
		i++;
		
	}
	imprimeVetor(10, &vet1);

	
	
	
	
	
	
	

}

 

funcoes.c

 

#include "hash.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int hashModelo(char *chav, int modulo){
	int h = 0;
	int n,i;
	n = strlen(chav);
	for(i=0;i<n;i++){
		h += chav[i]*31*n;
	}
	return h%modulo;
}

//funcao hash que utiliza as strings para formar uma, adicionado o modulo para se adaptar ao tamanho da tabela.

void imprimeVetor(int maximo, Chave *vet){
	int i =0;
	
	for(i=0;i<maximo;i++){
		printf("%d\n", vet->numero[i]);
		
	}
	
}

//funcao pra imprimir o valor com um maximo pré-determinado, para testes apenas.

 

hash.h

 

#ifndef HASH_H
#define HASH_H

typedef struct chave {
int numero;
char *string;
struct chave *prox; // será usado para o tratamento de colisão encadeado
} Chave;
#endif



void imprimeVetor(int maximo, Chave *vet);

int hashModelo(char *chav, int modulo);

 

 

To trancado nisso há dias já :(

 

É no imprimeVetor.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Correções:

#ifndef HASH_H
#define HASH_H

typedef struct chave
{
	int numero;
	char *string;
	struct chave *prox;
} Chave;

void imprimeVetor(int maximo, Chave *vet);
int hashModelo(char *chav, int modulo);
#endif
#include "hash.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define posicoes 100

int main(int argc, char** argv)
{
	/* int posicoes = 100; */
	Chave vet1[posicoes]; /* VLA é necessário? Se é necessário, malloc não substitui?
                                 É suportado pelo compilador? É apoiado pelo padrão de C,
                                 no qual você está compilando (c90, c99, c11...)? */
	/* int numeroElementos = 0; // Variável inútil */
	Chave novo;
	/* int fatorCarga = 0;  // Variável inútil. 0/100 = 0, divisão é necessária? */
	int i = 0;
	char aux[150], *aux1;
	int aux2;
	FILE *fp;



	if((fp = fopen( "chaves2M.txt", "r")) == NULL )
	{
		printf("Arquivo não encontrado\n");
		exit(1);
	}

	novo.numero = 1;
	novo.string = "lala";
	printf("%d %s", novo.numero, novo.string);


	while(!feof(fp) && i<10)
	{
		fgets(aux,150, fp);
		aux1 = strtok(aux, " ");
		aux2 = atoi(strtok(NULL, " "));
		novo.numero = aux2;
		novo.string = (char*) malloc(150 * sizeof(char));
		strcpy(novo.string,aux1);
		novo.prox = NULL;
		vet1[hashModelo(aux1, posicoes)] = novo;
		i++;
	}
	imprimeVetor(10, vet1);

	return 0;
}
#include "hash.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int hashModelo(char *chav, int modulo)
{
	int h = 0, n, i;

	n = strlen(chav);

	for(i=0;i<n;i++)
		h += chav[i] * 31 * n;

	return h % modulo;
}

void imprimeVetor(int maximo, Chave *vet)
{
	int i = 0;

	for(i = 0; i < maximo; i++)
	{
		printf("%d\n", vet[i].numero); /* vet->numero[i] está errado,
                                                pesquise um pouco sobre
                                                estruturas, vetores e suas
                                                correlações */
	}

}

Compartilhar este post


Link para o post
Compartilhar em outros sites

Opa, valeu ae Mateus, fiz algumas alterações no código.

 

Algumas variáveis são "inuteis" porque ainda não está pronto o código, posicoes não pode ser pelo define porque vai mudar com a função rehash, assim como o vetor não é alocado dinamicamente para maximizar o acesso a memória, a divisão estava no fatorDeCarga só para eu lembrar de fazer depois hehe, o fator de carga é numero de posições ocupadas / posições livres, quando ele chega no 0.8 ele faz a função rehash(que não está pronta).

 

Eu fiz as modificações que tu colocou, porém mesmo assim não funcionou, agora ele dar esse erro de compilação:

 

 

request for member ‘numero’ in something not a structure or union
Edit:
hahahaha
olha o erro que ridiculo
vet.numero e nao vet.numero

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.