Thread: Checking bounds array

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    3

    Checking bounds array

    Hi guys! First post haha.
    I'm kind of new to C so if I do anything really silly please let me know (C-related or forum related).

    So I have a 2-d array and I need to check the neighbouring cells are within the bounds of the array.
    For example, a 10x10 array should have max 8 neighbouring cells and at least 3.
    For now, I'm trying to do some arithmetic on the neighbouring cells but I'm having trouble since my loop keeps accessing cells outside of my defined array.

    This obviously happens at the cells on the sides and corner points, where I'm not getting 8 cells around the targetted cell.


    Is there a way to check if the cell is outside the array?

    tmp[r-1][c-1], tmp[r-1][c],
    tmp[r-1][c+1],
    tmp[r][c-1],
    tmp[r][c],
    tmp[r][c+1],
    tmp[r+1][c-1],
    tmp[r+1][c],
    tmp[r+1][c+1]);
    This is the code I'm using to read the array right now within 2 for loops.

    I know I need an if loop, but not sure how to go about it (I tried saying:
    "if r!=0" but then got lost...

    Any help would be great! Thanks

  2. #2
    Registered User
    Join Date
    Oct 2013
    Posts
    3
    Actually I think I may have a solution; can I test if the array is going to be cell -1? I think that should work.
    Don't know why that took me so long to figure out

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    -1 will work on the left/top, but not on the right/bottom. But using the same logic, you can figure out what number to check for.

  4. #4
    Registered User
    Join Date
    Oct 2013
    Posts
    3
    Yup I got it! Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array bounds checking
    By rohan_ak1 in forum C Programming
    Replies: 2
    Last Post: 08-26-2008, 10:16 PM
  2. Checking if pointer is in-bounds
    By zacs7 in forum C Programming
    Replies: 11
    Last Post: 04-14-2007, 04:46 AM
  3. array bounds checking
    By anykey in forum C++ Programming
    Replies: 2
    Last Post: 08-19-2005, 04:13 PM
  4. tile map bounds checking
    By Natase in forum Game Programming
    Replies: 1
    Last Post: 10-08-2003, 11:00 AM
  5. checking array bounds
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 05-06-2002, 02:29 AM

Tags for this Thread