【问题标题】:C programming array index out of boundC 编程数组索引越界
【发布时间】:2014-02-07 14:51:29
【问题描述】:

请给出您对数组越界的看法......如果我采用大数组输出对于 20 数组大小是正确的位......打印垃圾值

#include<stdio.h>
int main()
{
    int message[200],i,j,n,count,temp;
    count=0;
    scanf("%d",&n);
    //input taking line
    for(i=1; i<=n; i++)
        scanf("%d",&message[i]);
    for(i=1; i<=n; i++)
    {
        if(message[i]==1)
        {
            count+=1;
        }
        if(message[i]==0)
            count=0;

        if(count==5)
        {
            count=0;
            temp=message[i+1];
            message[i+1]=0;
            //n=n+1;
            //n1=n;
            for(j=(n); j<(i+2); j--)
            {
                message[j+1]=message[j];
            }
            n++;
            message[i+2]=temp;
        }
    }

    printf("line\n");
    //n1+=1;
    for(i=0; i<n; i++)
    {
        printf("%d\n",message[i]);
    }
    //n1+=1;
    return 0;

}

感谢您的帮助...

【问题讨论】:

    标签: c arrays arraylist indexoutofboundsexception


    【解决方案1】:

    在 C 中,数组索引从 0 开始。你的循环应该是这样的:

     for(i=0;i<n;i++) {...}
    

    【讨论】:

    • 如果我采用大数组,它仍然显示相同的错误..没有垃圾值..否则它显示垃圾值
    • n 的值不能大于200。
    • 您尝试运行所有 i,j 从零开始的程序.....并采用数组大小​​ 20 和 n=8 ...运行它...dn 给出解决方案
    猜你喜欢
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多