这段代码实现了一个十进制数转换为任意进制的程序,其中使用了指针和链表数据结构。

代码:

void DecimaltoN(Base* l) {
    Base* p;
    Base* t;
    char q[50];
    static char Start[] = "Start";
    static char End[] = "End";
    printf("输入Start开始进制转换,输入End结束:");
    gets_s(q);
    while (strcmp(q, Start) != 0) {
        if (strcmp(q, End) == 0) {
            printf("你取消了进制转换!");
            break;
        }
        printf("\n输入错误!输入Start开始进制转换:");
        memset(q, 0, sizeof q);
        gets_s(q);
    }
    while (strcmp(q, End) != 0) {
        int n, x, i, k;
        k = 0;
        char z = 'A';
        printf("\n输入要转换的数值与转换的进制:");
        scanf("%d %d", &x, &n);
        while (x != 0) {
            p = (Base*)malloc(sizeof(Base));
            Base* p = NULL; // 这里存在问题,重复定义了局部变量p
            i = x % n;
            p->record = i;
            if (i > 9) {
                p->record += z - 10;
            }
            else {
                p->record += '0';
            }
            p->top = l;
            l = p;
            x = x / n;
            k++;
        }
        t = l;
        if (t == NULL) {
            printf("错误!!");
        }
        else {
            printf("\n进制转换后:");
            for (i = 0; i < k; i++) {
                printf("%c", t->record);
                t = t->top;
            }
        }
        printf("\n输入要转换的值与转换的进制或者输入End结束进制转换:");
        l = NULL;
        free(p);
        memset(q, 0, sizeof q);
        gets_s(q);
        gets_s(q);
    }
}

问题分析:

代码中存在一个未初始化的本地指针变量 p。在循环 while (x != 0) 中,先定义了 p = (Base*)malloc(sizeof(Base));,然后紧接着又定义了 Base* p = NULL;,这会导致重复定义局部变量 p,并且覆盖了之前的定义,使得 p 指向一个未初始化的内存地址。

解决方案:

只需要删除第二行 Base* p = NULL; 即可。

修改后的代码:

void DecimaltoN(Base* l) {
    Base* p;
    Base* t;
    char q[50];
    static char Start[] = "Start";
    static char End[] = "End";
    printf("输入Start开始进制转换,输入End结束:");
    gets_s(q);
    while (strcmp(q, Start) != 0) {
        if (strcmp(q, End) == 0) {
            printf("你取消了进制转换!");
            break;
        }
        printf("\n输入错误!输入Start开始进制转换:");
        memset(q, 0, sizeof q);
        gets_s(q);
    }
    while (strcmp(q, End) != 0) {
        int n, x, i, k;
        k = 0;
        char z = 'A';
        printf("\n输入要转换的数值与转换的进制:");
        scanf("%d %d", &x, &n);
        while (x != 0) {
            p = (Base*)malloc(sizeof(Base));
            i = x % n;
            p->record = i;
            if (i > 9) {
                p->record += z - 10;
            }
            else {
                p->record += '0';
            }
            p->top = l;
            l = p;
            x = x / n;
            k++;
        }
        t = l;
        if (t == NULL) {
            printf("错误!!");
        }
        else {
            printf("\n进制转换后:");
            for (i = 0; i < k; i++) {
                printf("%c", t->record);
                t = t->top;
            }
        }
        printf("\n输入要转换的值与转换的进制或者输入End结束进制转换:");
        l = NULL;
        free(p);
        memset(q, 0, sizeof q);
        gets_s(q);
        gets_s(q);
    }
}

通过删除重复定义的语句,避免了指针变量 p 使用未初始化内存地址的问题。在修正后的代码中,p 变量被正确地初始化并分配了内存空间,确保了程序能够正常运行。

C 语言代码中的指针变量使用错误分析

原文地址: http://www.cveoy.top/t/topic/btDa 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录