pointers and arrays issue

This is a discussion on pointers and arrays issue within the C++ Programming forums, part of the General Programming Boards category; Okay, Im writing a program where I need a 2d array that I don't know the length and width of ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    25

    pointers and arrays issue

    Okay, Im writing a program where I need a 2d array that I don't know the length and width of at compiletime. I know the length and width will be the same, I just dont know what they are. Pointers only seem to work for making a 1d array. So if someone can help point me in the right direction!?

    (In case you were wondering, im writing a magic square program)

    Thanks!

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    25
    I found the following code:

    Code:
    int **ptr;
    	ptr=new int *[y];//creates an array pointer
    	for(int i=0; i<y; i++)
    	{
    		p[i]=new int [x];//creates space for each row
    	}
    however i dont know how to navigate through that, or what/when to declare p.

  3. #3
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    you can replace p with ptr, and you can access it like a static 2d array
    Code:
    ptr[row][col];

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    25
    Thanks but I figured it out on my own :-)

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    >> Im writing a program where I need a 2d array that I don't know the length and width of at compiletime.

    In C++ you should use a vector of vectors for this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  2. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 03:49 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 04:31 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21