CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2002
    Location
    CA
    Posts
    37

    General Array Question

    I wrote a simple program to find out some things about arrays. I found out what I needed to know, except when I cout the array, it gives me some weird numbers at the very end. I was just wondering if there is a problem with my code or what I can do to get rid of it. here's the code:

    ____________________________________________________

    #include <iostream.h>

    const int SIZE=10;

    void main()
    {
    int array[SIZE]={0};
    int value, num1, i;

    cout <<"Enter the section of the array:\n";
    cin >>value;

    cout <<"Enter the number to go into that section:\n";
    cin >>num1;

    array[value]=num1;

    for(i=0; i<=SIZE; i++)
    {
    cout <<array[i] <<endl;
    }
    }

  2. #2
    Join Date
    Jan 2002
    Location
    USA
    Posts
    150
    for(i=0; i<SIZE; i++)
    because for array of size 10 it used #'s 0-9 as index's, not 0-10: that would be an array of size 11

  3. #3
    Join Date
    Apr 1999
    Location
    Potsdam
    Posts
    110
    Since C/C++ does not perform any bounds checking, the compiler does not complain about you trying to access an element which lies outside your array. The for-loop goes one step to far and gets a memory location behind your array. What is found there is interpreted as integer but can be anything.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured