开发者社区 问答 正文

C-索引13'long long类型的出界

嗨,我是C的完整入门者。在任何论坛上,我都找不到下面的答案。

为什么我会出界错误。我试过在int和long long之间更改数组的类型,以查看它是否有所不同,但没有

#include <cs50.h>
#include <stdio.h>
#include <math.h>

int main (void)
{

 long long Cardnum;    
 long long n;
    int count = 0;
    printf("Enter Card Number: ");
    scanf("%lld", &Cardnum);
    n = Cardnum;
    while(n != 0)
    {
        // n = n/10
        n /= 10;
        ++count;
    }
    printf("Number of digits: %d\n", count);   

    if(count !=13 && count!=15 && count!=16)
    {

        printf("Invalid\n");
    }
    else     //Run luhns algo
    {
       printf("%lld\n",Cardnum); 
       long long numberArray[count]; 
      int c=0;
      int Digit=Cardnum; 

       while(Digit !=0) 
           {
           numberArray[count] = Digit%10;
           Digit/=10;
           c++;    
           }
    }


}


展开
收起
kun坤 2019-11-29 11:07:13 466 分享 版权
1 条回答
写回答
取消 提交回答
  • 您正在分配给numberArray[count],但是最后一个有效索引是numberArray[count - 1],因为索引始于0,所以count == 13有13个索引:0… 12(含)。也许您打算在numberArray[c]那里使用。

    还要注意,这long long是数组元素的类型,而不是整个数组或其索引的类型。索引以数组元素为单位,即,即使long long arr[n]内存大于uint8_t arr[n],它们都具有n索引/元素。在这种情况下,long long当您的元素是时,将是极大的过度杀伤力% 10。

    #https://stackoverflow.com/questions/59092446/c-index-13-out-of-bounds-for-type-long-long

    2019-11-29 11:07:26
    赞同 展开评论
问答分类:
问答标签:
问答地址: