user69514 user69514 - 5 months ago 16
C Question

C++ Pointers. How to assign value to a pointer struct?

I have the following struct:

typedef struct{
int vin;
char* make;
char* model;
int year;
double fee;

Then I create a pointer of type car

car *tempCar;

How do I assign values to the tempCar? I'm having trouble = 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.