[英]How to use pointers in C language
當我運行此代碼時,它返回Segmentation Fault (core dumped)
。 我嘗試了很多東西,但我相信我使用的指針是錯誤的。 我究竟做錯了什么?
要求:每個變量都必須是一個指針。
#include <stdio.h>
#include <stdlib.h>
int main() {
/* All the variables that needed to be defined */
int *i = malloc(sizeof(int));
int *number = malloc(sizeof(int));
int *failing_count = malloc(sizeof(int));
double *total = malloc(sizeof(double));
double *grade = malloc(sizeof(double));
failing_count = 0;
total = 0;
grade = 0;
/* User is asked to enter the number of grades that are received */
printf("Enter number of grades:\n ");
scanf("%d", number);
/* For Loop is run */
for (*i = 0; i < number; *i++) {
printf("Enter a grade:\n ");
scanf("%lf", grade);
if (*grade < 70) {
failing_count++;
}
*total = *total + *grade;
}
/* Program prints the results */
printf("Average grade is %lf\n", *total / *number);
printf("Number of failing grades is %d\n", failing_count);
return 0;
free(i);
free(number);
free(failing_count);
free(total);
free(grade);
}
在要使用所指向的值而不是指針的任何地方使用*
。
改變:
failing_count = 0;
total = 0;
grade = 0;
到:
*failing_count = 0;
*total = 0;
*grade = 0;
改變:
for(*i = 0; i<number; *i++){
到:
for(*i = 0; *i<*number; (*i)++){
改變:
failing_count++;
到:
(*failing_count)++;
改變:
printf("Number of failing grades is %d\n", failing_count);
到:
printf("Number of failing grades is %d\n", *failing_count);
在:
return 0;
free(i);
free(number);
free(failing_count);
free(total);
free(grade);
return
之后的語句永遠不會執行。 將return
移到最后。
而不是failing_count = 0;
你可能想做*failing_count = 0;
在您的代碼中,所有變量都是指針或地址。 如果分配這些變量,則更改它們指向的地址。 為了改變這些變量指向的值,你應該使用 *.
int* i = malloc(sizeof(int));
int* number = malloc(sizeof(int));
int* failing_count = malloc(sizeof(int));
double* total = malloc(sizeof(double));
double* grade = malloc(sizeof(double));
*failing_count = 0;
*total = 0;
*grade = 0;
...
您的代碼中缺少一些*
:
scanf()
和free()
。*i++
由於優先規則而無法完成這項工作:您應該編寫(*i)++
或簡單地*i += 1
。這是一個修改后的版本:
#include <stdio.h>
#include <stdlib.h>
int main() {
/* All the variables that needed to be defined */
int *i = malloc(sizeof(int));
int *number = malloc(sizeof(int));
int *failing_count = malloc(sizeof(int));
double *total = malloc(sizeof(double));
double *grade = malloc(sizeof(double));
*failing_count = 0;
*total = 0;
*grade = 0;
/* User is asked to enter the number of grades that are received */
printf("Enter number of grades:\n ");
scanf("%d", number);
/* For Loop is run */
for (*i = 0; *i < *number; *i += 1) {
printf("Enter a grade:\n ");
scanf("%lf", grade);
if (*grade < 70) {
*failing_count += 1;
}
*total += *grade;
}
/* Program prints the results */
printf("Average grade is %lf\n", *total / *number);
printf("Number of failing grades is %d\n", *failing_count);
free(i);
free(number);
free(failing_count);
free(total);
free(grade);
return 0;
}
請注意,由於目標是使所有內容都成為指針,因此您也可以將函數轉換為指針並將參數取消引用到scanf()
和free()
太:
#include <stdio.h>
#include <stdlib.h>
int main() {
/* All the variables that needed to be defined */
int *i = (*malloc)(sizeof(*i));
int *number = (*malloc)(sizeof(*number));
int *failing_count = (*malloc)(sizeof(*failing_count));
double *total = (*malloc)(sizeof(*total));
double *grade = (*malloc)(sizeof(*grade));
*failing_count = 0;
*total = 0;
*grade = 0;
/* User is asked to enter the number of grades that are received */
(*printf)("Enter number of grades:\n ");
(*scanf)("%d", &*number);
/* For Loop is run */
for (*i = 0; *i < *number; *i += 1) {
(*printf)("Enter a grade:\n ");
(*scanf)("%lf", &*grade);
if (*grade < 70) {
*failing_count += 1;
}
*total += *grade;
}
/* Program prints the results */
(*printf)("Average grade is %lf\n", *total / *number);
(*printf)("Number of failing grades is %d\n", *failing_count);
(*free)(&*i);
(*free)(&*number);
(*free)(&*failing_count);
(*free)(&*total);
(*free)(&*grade);
return 0;
}
failing_count = 0;
total = 0;
grade = 0;
您正在重新分配malloc
提供給您的指針,這意味着您丟失了對已分配內存的唯一引用,並且當您稍后增加、取消引用並嘗試free
指針時,您會出現未定義的行為。
您還應該始終檢查malloc
的返回值是否為NULL
以確保分配成功。
注意: free(NULL)
是良性的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.