Thread: 2d array problem with vc++

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    15

    2d array problem with vc++

    Hi, I am new to Visual C++.

    I have a 2d array in my function.
    char array[MAX][MAX];

    I then fill in the contents of the array, say, like this:
    array[0][0]='a';
    array[0][1]='b';
    array[0][2]='\0';

    Now when I run the debugger and want to view the contents of the 2D array,
    I don't see them.
    If I type this in my watch window:
    array[0]
    it says "error: index 0 out of bounds for pointer/array 'array' "
    same error comes up if I type something like this in my watch window:
    array[0],4

    Can anyone tell me what I am doing wrong? I really need to be able to view contents of my 2d array!

    Thanks for any ideas.

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    127

    Re: 2d array problem with vc++

    Originally posted by LiLgirL
    Hi, I am new to Visual C++.

    I have a 2d array in my function.
    char array[MAX][MAX];

    I then fill in the contents of the array, say, like this:
    array[0][0]='a';
    array[0][1]='b';
    array[0][2]='\0';

    Now when I run the debugger and want to view the contents of the 2D array,
    I don't see them.
    If I type this in my watch window:
    array[0]
    it says "error: index 0 out of bounds for pointer/array 'array' "
    same error comes up if I type something like this in my watch window:
    array[0],4

    Can anyone tell me what I am doing wrong? I really need to be able to view contents of my 2d array!

    Thanks for any ideas.
    type what in your watch window??
    array[0] !!!!

    for sure it won't work ... you are working on 2d array not 1d array


    but can you simply tell me what is your program about and what is required from the program so it can be more clear?
    Last edited by M_Ghani; 03-15-2004 at 02:20 PM.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    15
    well basically, i am trying to fill in a 2d array.

    when i run the debugger i just want to be able to view its contents.

    how do i do that?

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    127
    Originally posted by LiLgirL
    well basically, i am trying to fill in a 2d array.

    when i run the debugger i just want to be able to view its contents.

    how do i do that?
    you want to fill a 2d array
    well lets consider ur array is called array1[5][3] .. or the size you wish

    to fill array1 do the following
    Code:
    for (int i=0;i<5;i++)
     { 
        for (int j=0;j<3;j++)
         {
    
           cout<<"enter the number \n";
           cin>>array1[i][j];
         }
    }
    this is in order to fill 2d array of dimensions 5 and 3

    in order to display the first row on screen for instance

    Code:
    for(j=0;j<5;j++)
    
    	cout<<array1[0][j]<<" ";

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    15
    yea...i know how to print it and view the contents, but i want to know how to view the contents in the watch window of vc++.

    doing array[0][0] wont do it.

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    127
    excuse me but what do u mean by Watch window?

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    In Visual C++ 6.0, I was able to see "ab" when I put array[0] into my watch window while running the code below:
    Code:
    int main()
    {
        const int MAX = 4;
        char array[MAX][MAX];
    
        array[0][0]='a';
        array[0][1]='b';
        array[0][2]='\0';
    
        int checkWatchWindow = 3;
    }
    So it might be some other problem with your code. Maybe you should post more of it.

    -------

    M_Ghani, the watch window is something in the Visual C++ debugger which allows you to see tha value of a variable while the program is running. LiLgirL sounds like she (or he) is using it correctly, but there is some other problem.

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    15
    Ok, I made a new project with just your main fn, jlou. It still gaves the same error in my watch window. I am using vc++.NET...maybe thats a problem?


  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Sorry, I don't have .NET handy to test on. Maybe somebody else can try it out. Did you try &(array[0]) or &array[0]?

  10. #10
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    using the following code:
    Code:
    #include "stdafx.h"
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	const int MAX = 4;
        char array[MAX][MAX];
    
        array[0][0]='a';
        array[0][1]='b';
        array[0][2]='\0';
    	
    	return 0;
    }
    and the watch - array[0]. I could see all the characters being copied into the array by pressing f11. So I do not know why you are having the problem.
    Be a leader and not a follower.

  11. #11
    Registered User
    Join Date
    Sep 2003
    Posts
    15
    I finally got it to work by using a win32 console project.

    I've been using console application (.NET) and it didn't work there..

    Thanks for the ideas, people!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with 2D array
    By Stlcardinal50 in forum C++ Programming
    Replies: 1
    Last Post: 04-23-2009, 03:23 PM
  2. 8 Queens, problem with searching 2D array
    By Sentral in forum C++ Programming
    Replies: 35
    Last Post: 03-11-2009, 04:12 PM
  3. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  4. Copying from one 2d array to another....with a twist
    By Zildjian in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2004, 07:39 PM
  5. 2d array problem
    By LiLgirL in forum Windows Programming
    Replies: 1
    Last Post: 03-15-2004, 02:23 PM