2d array using pointers

This is a discussion on 2d array using pointers within the C++ Programming forums, part of the General Programming Boards category; The if(tilesetpath) and if(npclist) are what are unnecessary, so I removed them from the code I posted. If VC++ causes ...

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The if(tilesetpath) and if(npclist) are what are unnecessary, so I removed them from the code I posted. If VC++ causes you trouble with that, then you are doing something wrong. (BTW, VC++ 7.1 is one of the most standards-conformant compilers available.) Of course, I was assuming that tilesetpath and npclist were not arrays but regular pointers. If they are arrays you should put the [] in front of them instead of behind.

    To delete an array, you put the [] between the delete and the array name. Since you have a two-dimensional array, each value in the outer array is another array to be deleted. That is why I used delete [] grid[i];. The name of the array is grid[i] and you put delete [] in front of it. The outer array must also be deleted, and the outer array is just grid, so that is why I used delete [] grid;.

    The only other change I made was to loop from 0 to x, which as I said is because you have x rows (i.e. x inner arrays).

    While vectors have their own learning curve, you don't have to do anything with them in the destructor, their data will be cleaned up automatically.
    Last edited by Daved; 08-25-2005 at 10:32 PM.

  2. #17
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Yea I found a friend who was kind enough to go over all the pointer stuff with me. Thanks for all the help though.... vectors might be a better way to go but for now I want to learn pointers.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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