简体   繁体   English

C语言中指针的使用

[英]How to use pointers in C language

When I run this code, it comes back with Segmentation Fault (core dumped) .当我运行此代码时,它返回Segmentation Fault (core dumped) I have tried so many things but I believe I am using pointers wrong.我尝试了很多东西,但我相信我使用的指针是错误的。 What am I doing wrong?我究竟做错了什么?

Requirements: Every variable has to be a pointer.要求:每个变量都必须是一个指针。

#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); 
}

Use * everywhere you want to use the value that is pointed to instead of the pointer.在要使用所指向的值而不是指针的任何地方使用*

Change:改变:

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

to:到:

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

Change:改变:

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

to:到:

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

Change:改变:

                        failing_count++; 

to:到:

                        (*failing_count)++; 

Change:改变:

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

to:到:

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

In:在:

        return 0; 

        free(i); 
        free(number); 
        free(failing_count); 
        free(total); 
        free(grade); 

the statements after the return are never executed. return之后的语句永远不会执行。 Move the return to be last.return移到最后。

Instead of failing_count = 0;而不是failing_count = 0; you probably want to do *failing_count = 0;你可能想做*failing_count = 0; In your code, all variables are pointers or addresses.在您的代码中,所有变量都是指针或地址。 If you assign these variables, you change the address they point to.如果分配这些变量,则更改它们指向的地址。 In order to change the value pointed by these variables, you should use *.为了改变这些变量指向的值,你应该使用 *.

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; 
...

A number of * are missing in your code:您的代码中缺少一些*

  • every pointer must be dereferenced except when passed as such to scanf() and free() .每个指针都必须取消引用,除非这样传递给scanf()free()
  • Also beware that *i++ does not do the job because of precedence rules: you should write (*i)++ or simply *i += 1 .还要注意*i++由于优先规则而无法完成这项工作:您应该编写(*i)++或简单地*i += 1

Here is a modified version:这是一个修改后的版本:

#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; 
}

Note that since the goal is to make everything a pointer, you can also turn the functions to pointers and dereference the arguments to scanf() and free() too:请注意,由于目标是使所有内容都成为指针,因此您也可以将函数转换为指针并将参数取消引用到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; 

You are reassigning pointers that were given to you by malloc , meaning that you lose your only reference to the allocated memory and when you later increment, dereference, and try to free the pointers, you have undefined behavior.您正在重新分配malloc提供给您的指针,这意味着您丢失了对已分配内存的唯一引用,并且当您稍后增加、取消引用并尝试free指针时,您会出现未定义的行为。

You should also always check the return value of malloc is not NULL to ensure that allocation succeeded.您还应该始终检查malloc的返回值是否为NULL以确保分配成功。

Note: free(NULL) is benign.注意: free(NULL)是良性的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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