sprintf“访问冲突读取位置”异常

问题描述

我正在学习C语言的编程课程,无论遇到多大的努力,我都遇到了一个无法解决的错误。

该程序指示我: “向用户询问EAN编号的前12位数字,然后通过执行以下操作来计算第13位“安全”数字:

添加第二,第四,第六,第八,第十和第十二位数字 添加第一,第三,第五,第七,第九和第十一位数字 将第一个和乘以3,然后将其加到第二个和 从总数中减去1 将调整后的总数除以10后,计算余数 从9减去余数

我来自Python,因此将数字拆分成单个数字的最简单方法是将其转换为字符串,然后将字符数组中的每个元素还原为存储在整数数组中的int。

到目前为止,这是我的代码:

#include <stdio.h>

int main(void)
{
    long ean;       //creates variable to store a 12 digit integer
    int digits[12] = { 0,0 }; //initializes a 1x12 integer array
    printf("Please enter the first 12 digits in your EAN: ");
    scanf_s("%ld",&ean);   //collects the appropriate user input and stores it in ean
    char seperated[12];     //creates character array
    **sprintf_s(seperated,"%ld",ean); //converts the long integer ean to a string called separated
    for (int i = 0; i < 12; i++)
    {
        sscanf_s(seperated[i],"%d",&digits[i]);   //iterates through the characters and converts each one to an integer
                                                    //stored in the array created earlier
        printf("%d\n",digits[i]);
    }
    int sumEvens = digits[1] + digits[3] + digits[5] + digits[7] + digits[9] + digits[11]; //sums the even digits
    int sumOdds = digits[0] + digits[2] + digits[4] + digits[6] + digits[8] + digits[10]; //sums the odd digits

    int total = (3 * sumEvens + sumOdds)-1;     //multiplies the first sum by 3 and adds it to the second sum,//then subtracts one from the total
    int securityDigit = 9 - (total % 10);   //Computes the remainder when the total is divided by 10 and subtracts that from 9
    return 0;
}

问题是,我的sprintf语句(带有**标记)抛出了异常提示

Exception thrown at 0x0FB7373A (ucrtbased.dll) in ECE1400_2.exe: 0xC0000005: Access violation reading location 0xBE991A6D.

有人知道我在做什么错以及如何解决吗?我希望这只是一点点,但我一直盯着它并进行了2个小时以上的研究。

解决方法

有很多不必要的部分。

int main()
{
    char s[12] = {0};
    int n = 0;
    int digits[12] = { 0,0 }; //initializes a 1x12 integer array
    printf("Please enter the first 12 digits in your EAN: ");
    for (int i = 0; i < 12; i++)
    {
        scanf("%d",&digits[i]);
    }
    int sumEvens = digits[1] + digits[3] + digits[5] + digits[7] + digits[9] + digits[11]; //sums the even digits
    int sumOdds = digits[0] + digits[2] + digits[4] + digits[6] + digits[8] + digits[10]; //sums the odd digits

    int total = (3 * sumEvens + sumOdds)-1;     //multiplies the first sum by 3 and adds it to the second sum,//then subtracts one from the total
    int securityDigit = 9 - (total % 10);   //Computes the remainder when the total is divided by 10 and subtracts that from 9
    
    for (int i = 0; i <= 11; i++) {
        n += sprintf (&s[n],"%d",digits[i]);
    }

}
,

您可能想做这样的事情

int main(void)
{
    int a[5]={1,2,3,1,3};
    char s[9] = {0};
    int n = 0;

    for (int i = 0; i < 5; i++) {
        n += sprintf (&s[n],a[i]);
    }

    printf ("\n char* s = %s\n\n",s);

    return 0;
}

不要太长。

,您无需使用注释中提到的sscanf_s。

,

这将是一个开始:

int main(void)
{
  __int64  ean;                       // using 64 bit integer

  int digits[12] = { 0 };             // one zero is enough

  printf("Please enter the first 12 digits in your EAN: ");
  scanf_s("%lld",&ean);              // using %lld here because ean is a 64 bit type
  char seperated[12];

  sprintf_s(seperated,12,"%lld",ean); // also using %lld

  for (int i = 0; i < 12; i++)
  {
    digits[i] = seperated[i] - '0';   // I let you find out yourself about this "magic"
    printf("%d\n",digits[i]);
  }
  ...

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...