c++指针。如何给指针结构赋值?

时间:2021-09-10 21:19:19

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的指针

car *tempCar;

How do I assign values to the tempCar? I'm having trouble

如何为tempCar赋值?我有困难

        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

编译器一直说tempCar类型*。我不知道我做错了什么

6 个解决方案

#1


25  

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.

另外,如果您正在使用这样的指针,请不要忘记为tempCar分配内存。这就是“新”和“删除”的功能。

#2


13  

You have to dereference the pointer first (to get the struct).

您必须首先取消引用指针(以获取结构体)。

Either:

:

(*tempCar).make = "GM";

Or:

或者:

tempCar->make = "GM";

#3


5  

tempCar->vin = 1234

tempCar - > vin = 1234

The explanation is quite simple : car* is a pointer on car. It's mean you have to use the operator -> to access data. By the way, car* must be allocated if you want to use it.

解释很简单:car*是car上的指针。这意味着您必须使用操作符—>来访问数据。顺便说一下,如果你想要使用汽车的话,必须分配汽车*。

The other solution is to use a declaration such as car tempCar;. The car struct is now on the stack you can use it as long as you are in this scope. With this kind of declaration you can use tempCar.vin to access data.

另一种解决方案是使用声明,如car tempCar;汽车结构体现在在堆栈上,只要您在此范围内,就可以使用它。有了这种声明,您可以使用tempCar。vin访问数据。

#4


1  

Your tempCar is a pointer, then you have to allocate memory for it and assign like this:

你的tempCar是一个指针,然后你必须为它分配内存,并像这样分配:

tempCar = new car();
tempCar->vin = 1234;         
tempCar->make = "GM";
tempCar->year = 1999;
tempCar->fee = 20.5;

Otherwise declare tempCar in this way: car tempCar;

否则以这种方式申报tempCar: car tempCar;

#5


-1  

Change your car *temp to below line:

把你的车换到下面行:

 car *tempCar = (car *)malloc(sizeof(car));

 tempCar->vin = 1234;         
 tempCar->make = "GM";
 tempCar->year = 1999;
 tempCar->fee = 20.5;

#6


-2  

People, be careful when using new, this is not Java, it's C++, don't use parentheses when you don't have parameters: tempCar = new car;

大家,使用new时要小心,这不是Java,是c++,没有参数时不要使用圆括号:tempCar = new car;

#1


25  

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.

另外,如果您正在使用这样的指针,请不要忘记为tempCar分配内存。这就是“新”和“删除”的功能。

#2


13  

You have to dereference the pointer first (to get the struct).

您必须首先取消引用指针(以获取结构体)。

Either:

:

(*tempCar).make = "GM";

Or:

或者:

tempCar->make = "GM";

#3


5  

tempCar->vin = 1234

tempCar - > vin = 1234

The explanation is quite simple : car* is a pointer on car. It's mean you have to use the operator -> to access data. By the way, car* must be allocated if you want to use it.

解释很简单:car*是car上的指针。这意味着您必须使用操作符—>来访问数据。顺便说一下,如果你想要使用汽车的话,必须分配汽车*。

The other solution is to use a declaration such as car tempCar;. The car struct is now on the stack you can use it as long as you are in this scope. With this kind of declaration you can use tempCar.vin to access data.

另一种解决方案是使用声明,如car tempCar;汽车结构体现在在堆栈上,只要您在此范围内,就可以使用它。有了这种声明,您可以使用tempCar。vin访问数据。

#4


1  

Your tempCar is a pointer, then you have to allocate memory for it and assign like this:

你的tempCar是一个指针,然后你必须为它分配内存,并像这样分配:

tempCar = new car();
tempCar->vin = 1234;         
tempCar->make = "GM";
tempCar->year = 1999;
tempCar->fee = 20.5;

Otherwise declare tempCar in this way: car tempCar;

否则以这种方式申报tempCar: car tempCar;

#5


-1  

Change your car *temp to below line:

把你的车换到下面行:

 car *tempCar = (car *)malloc(sizeof(car));

 tempCar->vin = 1234;         
 tempCar->make = "GM";
 tempCar->year = 1999;
 tempCar->fee = 20.5;

#6


-2  

People, be careful when using new, this is not Java, it's C++, don't use parentheses when you don't have parameters: tempCar = new car;

大家,使用new时要小心,这不是Java,是c++,没有参数时不要使用圆括号:tempCar = new car;