简体   繁体   中英

using malloc with a float variable in struct

For my class we're practicing the use of malloc and memcpy to take inputs into a struct for a made up data base. Where you type in float, and two separate char's. The issue im running into here is that when im trying to malloc for the float input its giving me an error saying a value of type "float*" cannot be assigned to an entity of type "float" . In my struct declaration i have it declared as just float price , but the problem i run into is that if i change it to float price* , the variables i used to prefill the struct error out saying a value of type "double" cannot be used to initialize an entity of type "float*" .

I have also tried doing malloc(sizeof(price)); just by itself with out the float cast but that doesnt seem to get me anywhere either

Am i missing something when it comes to how pointers work? Is their a propper way to malloc with a float variable or is it done automatically?

void getInfo(struct movieRental *temp){


float price;
printf("Please type in a price ");
scanf("%f", &price);
temp->price = (float* ) malloc(sizeof(price));
memcpy(temp->price, &price, sizeof(price));
}

struct movieRental{

char *title;
char *director;
float price; //changing this to "float *price" errors out my prefilled structs for a later function
};


struct movieRental rental1 = { 
    "Indiana Jones and the last Crusade",
    "Steven Spielberg", 
    12.55,

};


struct movieRental rental2 = {
    "Star Wars Episode 4",
    "George Lucas",
    24.75,


};



struct movieRental rental3 = {
    "Office Space",
    "Mike Judge",
    45.50,
};

You misunderstand how you should allocate memory. The price member is a part of the movieRental struct. You don't need to allocate memory here since the getInfo function already receives a pointer to a movieRental struct, so it is not the responsibility of the function.

Instead, you can read the data straight into the struct member:

scanf("%f", &temp->price);

I would recommend picking better variable names than temp .

You have float price in the structure; you don't need to, and shouldn't attempt to, allocate memory for the price. Simply store the value in the variable. You could, in theory, use float *price in the structure and then you would need to allocate memory for the variable, but doing so would be pointless, using space unnecessarily.

On most systems, sizeof(float) == 4 . If you're using a 32-bit system, the float would be the same size as the float * , so you'd incur the memory overhead of an unnecessary pointer and more complicated code to access the price (access would be slower). If you're using a 64-bit system, the pointer is twice as big as the float and the waste is that much larger. There's also overhead associated with each memory allocation.

Your function could be:

void getInfo(struct movieRental *temp){
    printf("Please type in a price ");
    if (scanf("%f", &temp->price) != 1)
    {
        …handle error…but how?
    }
}

You could read into a local variable and then simply assign the local variable to the structure element, but it really isn't needed. For the error handling, you might be better off changing the function return type to int (or bool ) so you can indicate success or failure. You should probably also rename the function; it gets the price, and 'info' is a woolly term compared with 'price'.

int getPrice(struct movieRental *temp){
    float price;
    printf("Please type in a price ");
    if (scanf("%f", &temp->price) != 1)
        return -1;
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM