简体   繁体   中英

How to use pointers in C language

When I run this code, it comes back with 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. Move the return to be last.

Instead of failing_count = 0; you probably want to do *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() .
  • Also beware that *i++ does not do the job because of precedence rules: you should write (*i)++ or simply *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:

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

You should also always check the return value of malloc is not NULL to ensure that allocation succeeded.

Note: free(NULL) is benign.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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