You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言中使用++增量运算符操作int变量结果异常求助

增量运算符*records++使用异常问题分析

我在将int类型变量records的地址传递给open()函数(该函数以二进制模式打开文件并统计struct record元素数量)时,遇到了增量运算符的异常问题:

异常现象(使用*records++;)

文件实际包含4条记录,但循环内打印结果为:

HELLO! 1
HELLO! 0
HELLO! -251258176
HELLO! 22074

回到main函数后打印:

Records in file: 0.

正常现象(使用*records += 1;)

循环内打印结果符合预期:

HELLO! 1
HELLO! 2
HELLO! 3
HELLO! 4

main函数打印:

Records in file: 4.

相关代码

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

#define true 1
#define false 0
#define size 100

typedef struct record {
    char firstName[size], lastName[size];
    int age;
    double grade;
} record;

int open(const char *filename, FILE **fp, int *records);

int main() {
    FILE *fp;
    char filename[] = "records.dat";
    int records = 0, opened = false, ch = 1;

    while(ch > 0 && ch < 5) {

        printf("Select your choice from the list below: \n");
        printf("1. Open file and get amount of records in it\n");
        printf("Enter your choice: ");
        scanf("%d", &ch);
        while(getchar() != '\n');

        switch(ch) {
            case 1:
                if(opened) {
                    printf("\nFile already open, try closing it first please.\n");
                    break;
                }

                opened = open(filename, &fp, &records);

                if(opened) {
                    printf("\nRecords in file: %d.\n", records);
                } else { // Failed to open AND create file
                    return 1;
                }

                break;

            default:
                printf("\nGoodbye!\n");
                if(opened) {
                    fclose(fp);
                }
                return 0;
                break;
        }
    }
}

int open(const char *filename, FILE **fp, int *records) {
    char temp[size];
    record r;
    *records = 0;

    printf("Attempting to open file...\n");
    *fp = fopen(filename, "rb+");
    if(*fp == NULL) {
        printf("File doesn't exit, attempting to create file...\n");
        *fp = fopen(filename, "wb+");
        if(*fp == NULL) {
            fprintf(stderr, "Couldn't open neither create %s, exiting...\n", filename);
            return false;
        }
        printf("File created.\n");
        return true;
    }
    printf("File found!\n");

    while(fread(&r, sizeof(r), 1, *fp) == 1) {
        *records++;
        //*records += 1;
        printf("HELLO! %d\n", *records);
    }

    return true;
}

问题原因

核心是运算符优先级:++的优先级高于*,所以*records++等价于*(records++)。执行逻辑变成:

  1. 先将指针records本身向后偏移一个int的长度(指向了未知内存地址)
  2. 再对偏移后的指针进行解引用取值

这就导致:

  • main函数中传入的records变量地址根本没被修改,所以最终值还是初始的0
  • 循环内打印的*records是偏移后指针指向的随机内存数据,因此出现乱码

解决方案

如果要使用自增运算符,必须添加括号明确优先级,写成(*records)++。这样会先解引用指针找到main函数中的records变量,再对该变量执行自增操作,效果和*records +=1完全一致。

内容的提问来源于stack exchange,提问作者Agent 47

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 14:19:57