如何在C语言中使用指针

3

当我运行这段代码时,它返回了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); 
}

1
请检查total = 0;。你可能需要使用*total = 0; - chux - Reinstate Monica
称呼指针为 i 是在自找麻烦 :) - chqrlie
4个回答

6

在需要使用指针所指的值而非指针本身的地方,全部使用 *

修改为:

        failing_count = 0; 
        total = 0; 
        grade = 0; 

to:

        *failing_count = 0; 
        *total = 0; 
        *grade = 0; 

变更:

        for(*i = 0; i<number; *i++){ 

to:

        for(*i = 0; *i<*number; (*i)++){ 

变更:

                        failing_count++; 

致:

                        (*failing_count)++; 

变更:

        printf("Number of failing grades is %d\n", failing_count); 

to:

        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移到最后。


3

与其使用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; 
...

2

你的代码中缺少一些 *

  • 每个指针都必须被解引用,除非作为参数传递给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; 
}

0
failing_count = 0; 
total = 0; 
grade = 0; 

你正在重新分配由malloc给你的指针,这意味着你失去了对已分配内存的唯一引用,当你稍后增加、解除引用和尝试free指针时,会出现未定义的行为。

你还应该始终检查malloc的返回值不是NULL,以确保分配成功。

注意:free(NULL)是无害的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接