confusion between * and &
why T& and *f;
Time stamp 6:40
also expain what does f holds
T& operator [](string key){
T* f=search(key);
if(f==NULL){
insert(key,0);
f=search(key);
}
return *f;
}
f is a pointer of type T which holds the address of node containing key
now we have to return address of node so we return value inside pointer which is *f;
as we are returning address or reference of node so return type will be T&
if you have more doubts regarding this feel free to ask
i hope this helps
if yes hit a like : and don’t forgot to mark doubt as resolved
what does *f hold here
as told it is pointer
which contains the address of that node whose data==key
which we get form search function
T* search(string key){
int idx=hashFn(key);
node*temp=table[idx];
while(temp!=NULL){
if(temp->key==key){
//returns address where value is stored
return &temp->value;
}
temp=temp->next;
}
return NULL;
}
this is the search function so f is address of value
so *f should be value?
so how t& and *f have same data type
and the how does price["key]=10 gives correct result
in the operator[] overloading function
we store this address in the pointer f
so inside operator[] overloading function
f is pointer
*f is address
f should be a pointer but *f should be a value?
as in search function we return &temp->value
yes it is a value
but the pointer contains address
so the value of f is address
i hope now it is clear
noooooooooooooooooooooooooooooooo
when i do cout<<f;
what will i get
and when i do cout<<*f;
cout<<f
give error as it is pointer
cout<<*f
this will print address of node
sorry but i am unable to understand
exactly which thing you don’t understand
#include
using namespace std;
int main() {
int y=10;
int*x;
x=&y;
cout<<x;
}
here x is giving an address
but u are saying it will be error
okay right
now i am briefing the whole discussion
f is pointer
*f is value of pointer which is key
now we are returning *f by reference as return type is T& so that we can update value in main also
now got it thanksssssss