I have the following struct:
typedef struct{
int vin;
char* make;
char* model;
int year;
double fee;
}car;
Then I create a pointer of type car
car *tempCar;
How do I assign values to the tempCar? I'm having trouble
tempCar.vin = 1234;
tempCar.make = "GM";
tempCar.year = 1999;
tempCar.fee = 20.5;
Compiler keeps saying tempCar is of type car*. I'm not sure what I'm doing wrong
You need to use the -> operator on pointers, like this:
car * tempCar = new car();
tempCar->vin = 1234;
tempCar->make = "GM";
//...
delete tempCar;
Also, don't forget to allocate memory for tempCar if you're using a pointer like this. That's what 'new' and 'delete' do.
You have to dereference the pointer first (to get the struct).
Either:
(*tempCar).make = "GM";
Or:
tempCar->make = "GM";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With