提供特定值,但返回错误的指针

时间:2018-11-17 00:42:37

标签: c pointers if-statement struct

我有一个包含不同等级单词的结构。 例如,如果您获得A级成绩,则描述您的单词是“辉煌”,“很棒”。如果您获得B级,则描述您的单词是“好”,“不错”。这些单词存储为结构。

但是,我需要创建一个函数(public void onPause() { super.onPause(); overridePendingTransition(0, 0); } ),在其中解析年级,以及该年级的头还是尾,然后返回该年级单词的结构。但是,无论年级如何,它总是使我返回相同的指针。如果我给它尾部的等级为A,它将返回我的指针100。如果我给它尾部的等级为B,它将返回我的指针100。

grade_pnter_finder

输出:

#include <stdio.h>
#include <stdlib.h>

struct grade {
    struct grade_word *A_head;
    struct grade_word *A_tail;

    struct grade_word *B_head;
    struct grade_word *B_tail;

    struct grade_word *C_head;
    struct grade_word *C_tail;

    struct grade_word *D_head;
    struct grade_word *D_tail;

    struct grade_word *E_head;
    struct grade_word *E_tail;

};

struct grade_word {
    char *word;
    int word_len;
    struct grade_word *next;
    struct grade_word *prev;
};

struct grade *create_grade() {

    struct grade *new_grade = malloc(sizeof(struct grade));

    // Check grade was allocated correctly
    if (new_grade == NULL) {
        fprintf(stderr, "ERROR: Could not allocate memory for grade\n");
        exit(1);
    }

    // Initialise all variables
    new_grade->A_head = NULL;
    new_grade->A_tail = NULL;
    new_grade->B_head = NULL;
    new_grade->B_tail = NULL;
    new_grade->B_head = NULL;
    new_grade->C_tail = NULL;
    new_grade->C_head = NULL;
    new_grade->C_tail = NULL;
    new_grade->D_head = NULL;
    new_grade->D_tail = NULL;
    new_grade->E_head = NULL;
    new_grade->E_tail = NULL;

    return new_grade;
}

struct grade_word *grade_pnter_finder(struct grade *grade_data, char grade,
                                      char h_or_t) {

    struct grade_word *target = NULL;
    if (grade == 'A') {
        if (h_or_t == 'H') {
            target = grade_data->A_head;
        } else {
            target = grade_data->A_tail;
        }
    } else if (grade == 'B') {
        if (h_or_t == 'H') {
            target = grade_data->B_head;
        } else {
            target = grade_data->B_tail;
        }
    } else if (grade == 'C') {
        if (h_or_t == 'H') {
            target = grade_data->C_head;
        } else {
            target = grade_data->C_tail;
        }
    } else if (grade == 'D') {
        if (h_or_t == 'H') {
            target = grade_data->D_head;
        } else {
            target = grade_data->D_tail;
        }
    } else {
        if (h_or_t == 'H') {
            target = grade_data->E_head;
        } else {
            target = grade_data->E_tail;
        }
    }

    if (h_or_t == 'H') printf("Grade %c's Head pointer is at: %p\n", grade, &target);
    else printf("Grade %c's Tail pointer is at: %p\n", grade, &target);
    return target;
}

int main(void) {

    struct grade *new_grade = create_grade();
    struct grade_word *pnter;
    pnter = grade_pnter_finder(new_grade, 'A', 'H');
    pnter = grade_pnter_finder(new_grade, 'A', 'T');
    pnter = grade_pnter_finder(new_grade, 'B', 'H');
    pnter = grade_pnter_finder(new_grade, 'B', 'T');
    pnter = grade_pnter_finder(new_grade, 'C', 'H');
    pnter = grade_pnter_finder(new_grade, 'C', 'T');
    pnter = grade_pnter_finder(new_grade, 'D', 'H');
    pnter = grade_pnter_finder(new_grade, 'D', 'T');
    pnter = grade_pnter_finder(new_grade, 'E', 'H');
    pnter = grade_pnter_finder(new_grade, 'E', 'T');

    printf("Stop compiler complaining for unused variable = %p\n", &pnter);


}

如您所见,所有这些都指向同一个地方。它不应该指向不同的地方吗?

2 个答案:

答案 0 :(得分:1)

如果您引用的是以下印刷的值:

if (h_or_t == 'H') printf("Grade %c's Head pointer is at: %p\n", grade, &target);
else printf("Grade %c's Tail pointer is at: %p\n", grade, &target);

您应将&target更改为target。您要打印局部变量的值,而不是它在内存中存储的位置。

答案 1 :(得分:1)

已发布的代码为每个打印的地址显示相同的地址,因为它每次都打印&target的值。但是,target本身是指向grade_word结构的指针,具有自己的地址。是打印的target的地址,而不是target保留的值 ,即target指向的地址。

请注意,按照此代码打印地址,例如printf("Grade %c's Tail pointer is at: %p\n", grade, &target);的使用会导致根据C标准的未定义行为,该标准表示"the argument shall be a pointer to void."

%p转换说明符

相反,此代码应为:

printf("Grade %c's Tail pointer is at: %p\n", grade, (void *) target);

即使解决了上述两个问题,也请注意,新创建的grade_word结构中的指针已全部初始化为NULL,但都没有将它们设置为指向有意义的任何地方。