Home > Blockchain >  Pointers help for c
Pointers help for c

Time:11-11

I am new to programming and I was wondering if someone could help me out. My error says

[Error] cannot convert 'float**' to 'float*' for argument '1' to 'void acceptDailyandConvert(float*, float*, float*)'

and this is my code.

Thanks so much in advance.


void acceptDailyandConvert(float*, float*, float*);

int
main()
{
    
    // VARIABLE DECLARATION
    float *dailyRate, *hourlyRate, *minRate;
    
    // FUNCTION CALLING
    acceptDailyandConvert(&dailyRate, &hourlyRate, &minRate);
}

void
acceptDailyandConvert(float *dailyRate, float *hourlyRate, float *minRate){
    
    // accepts daily rate, converts into hourly rate and rate per minute (pointer variables)
    
    printf("Enter your daily rate: ");
    scanf("%f", &dailyRate);
    
    // hourly rate is 1/8th of daily rate
    hourlyRate = dailyRate * 0.125;
    // minute rate is 1/60th of hourly rate
    minRate = hourlyRate * 0.02;
    
    printf("\n \t Hourly Rate: %.2f ", hourlyRate);
    printf("\n \t Rate per minute: %.2f ", minRate);
    
}

Im not really sure how to go about it, ive tried mixing around the ampersand and the asterisk (Totally clueless).

CodePudding user response:

Ahhh yes, the pain of learning c pointers!!

This is one way to do it...

#include <stdio.h>

void acceptDailyandConvert(float*, float*, float*);

int
main()
{

// VARIABLE DECLARATION
    float dailyRate, hourlyRate, minRate;

// FUNCTION CALLING
    acceptDailyandConvert(&dailyRate, &hourlyRate, &minRate);
}

void acceptDailyandConvert(float *dailyRate, float *hourlyRate, float *minRate){

// accepts daily rate, converts into hourly rate and rate per minute (pointer variables)

    printf("Enter your daily rate: ");
    scanf("%f", dailyRate);

// hourly rate is 1/8th of daily rate
    hourlyRate[0] = dailyRate[0] * 0.125;
// minute rate is 1/60th of hourly rate
    minRate[0] = hourlyRate[0] * 0.02;

    printf("\n \t Hourly Rate: %.2f ", hourlyRate[0]);
    printf("\n \t Rate per minute: %.2f ", minRate[0]);

}

CodePudding user response:

If you want to use pointers, initialize them first, then pass the pointers to the function not the reference (remove the '&'). To access the pointer value use '*', eg. '*hourlyRate'.

  •  Tags:  
  • c
  • Related