C - sprintf "access violation reading location"异常



我正在上一堂C语言的编程课,遇到了一个无论我怎么努力都无法解决的错误。

程序指示我:"向用户询问EAN号码的前12位,然后计算第13位"EAN";安全性";通过执行以下操作进行数字:

将第二、第四、第六、第八、第十和第十二位数字相加将第一、第三、第五、第七、第九和第十一位数字相加将第一个和乘以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, 0, 0, 0, 0, 0, 0, 0, 0, 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("%dn", 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.

有人知道我做错了什么吗?我该怎么解决?我希望这只是一点点,但我已经盯着这个研究了两个多小时。

有很多不必要的部分。

int main()
{
char s[12] = {0};
int n = 0;
int digits[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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], "%d", a[i]);
}
printf ("n char* s = %snn", s);
return 0;
}

而不是让它变长。

您没有必要使用评论中提到的sscanfs。

这将是一个开始:

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("%dn", digits[i]);
}
...

最新更新