Hy all. I have this code, done it so, because it is a school project, so I am forced to use this things ...
Anyway, I don't understand what its wrong
Here is the code:
//this is valuta.h:
Code:
typedef void* cheie;
typedef void* valoare;
typedef int (*FunCmp)(cheie a, cheie b);
struct _Tabel;
typedef _Tabel* Tabel;
struct _Tabel{
cheie valuta[10];
valoare lei[10];
int dim;
FunCmp FC;
};
void creaza(Tabel& t, FunCmp f);
valoare insereaza(Tabel &t, cheie c, valoare v);
valoare cauta(Tabel& t,cheie c);
//this is valuta.cpp:
Code:
#include <iostream.h>
#include "valuta.h"
void creaza(Tabel& t, FunCmp f){
t=new _Tabel;
t->dim=-1;
}
valoare cauta(Tabel& t, cheie c){
while(t->dim>=0)
{
int i=t->dim;
cout<<"t->dim: "<<t->dim<<" value:"<<(*(int*)t->lei[i])<<endl;
//here is the big problem ... i
//guess that t->lei[i] point to the
//start off the array, but how can I
//make it to point to each element ? :-/
t->dim--; //in this moment this code just prints
//the last value for t->dim times ...
}
return 0;
}
//this is were I populate my struct ...
valoare insereaza(Tabel& t, cheie c, valoare v){
t->dim++;
t->valuta[t->dim]=c;
t->lei[t->dim]=v;
cout<<"t->dim:"<<t->dim<<endl;
cout<<(char*)t->valuta[t->dim];
cout<<" "<<(*(int *)t->lei[t->dim])<<endl;
return v;
}
// and finally this is the main program, here
//I create values and call the inserting function...
//I also call "cauta" wich should search for a value ...
//but you can see that is not working ...
Code:
#include "valuta.h"
#include <iostream.h>
void main(){
Tabel t;
FunCmp Fc;
creaza(t, Fc);
char val[10];
int *plei;
int lei;
int nrval;
cout<<"Cate valori ? (Maxim 10) ";//the number of values
cin>>nrval;
for (int i=0;i<nrval;i++){
cout<<"Valuta dorita:";//one value, a string
cin>>val;
cout<<endl;
cout<<"Valoarea in lei:";//another value, an int
cin>>lei;
cout<<endl;
plei=&lei;
insereaza(t, &val, plei);
}
cauta(t,val);
}
If I cout with:
cout <<"t->dim: " <<t->dim <<" value:" <<t->lei[t->dim]
it prints a memory address (the same )for 3 times (suposing t->dim = 3).
If I cout with:
cout <<"t->dim: " <<t->dim <<" value:" <<(*(int*)t->lei[i]) <<endl; //insteed of "i" I also may put t->dim, the result its
//the same
it prints the LAST value entered 3 times (suposing t->dim =3)
Is not this the problem, in first case it prints an adrress because t->lei[t->dim] (or t->lei[i]) is a pointer to struct.
In the second case it prints the value because the present of the dereference operator. Still in this case I don't understand why I can't wrote (*(int)t->lei[i]) because, its make sense for me. I cast to int, and I want to print the data from that location of memory ... Wright ? The compiler says: illegal indirection, but I don't really understand why ...
So how can I make things that I can print the data from every element ?
Any idea will be greatly apreciated ! Thank you!