나는 c 프로그래밍입니다. 기로 결정 나는 것을 배울 수행하여 문제의 일부 설정에서 cs50 오픈 코스. 다음 코드를 생산하고 세분화류(코어 버)오류가 있습니다. 나는 이유를 이해할 수 없다. 가 segmentation 오류가 함께 할 수 있다 액세스하는 메모리 당신은에 액세스 할 수 없습니다. 나는 보지 않는 것을 일으키는 원인이 될니다. 나는 가정으로 할 수 있다 포인터입니다. 나는 새로운 포인터입니다. 감사합니다.
#include <stdio.h>
// https://cs50.harvard.edu/x/2021/labs/1/population/
float yearly_llamas(float starting_population) {
// returns number of llamas at the end of the year
float born = starting_population / 3;
float died = starting_population / 4;
float end_of_year_pop = starting_population + born - died;
return end_of_year_pop;
}
int main(void) {
// use floats for precision
float *start_population;
float *end_population;
// set start lower limit
int start_min = 9;
// make sure input for starting population is greater than or equal to 9
do {
printf("Starting population: ");
scanf("%f", start_population);
} while (*start_population < start_min);
// get ending population, make sure greater than or equal to the starting population
do {
printf("Ending population: ");
scanf("%f", end_population);
} while (*end_population < *start_population);
// print for verification
printf("%f\n", *start_population);
printf("%f\n", *end_population);
float end_pop = yearly_llamas(*start_population);
printf("Llamas at the end of the year: %f\n", end_pop);
return 0;
}