Thread: Calloc for an array of array with negative index in C

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

    Calloc for an array of array with negative index in C

    I have an array of array with negative index. It is an array which has real dimensions [dim_y + 40][dim_x + 40] but the user uses the array like it has dimensions [dim_y][dim_x].
    So i see the array's rows lets say from -20 to dim_y + 20 but a user sees only from 0 to dim_y.
    First i had global and already defined the dimensions dim_x, dim_y, so i had this:

    Code:
    int map_boundaries[dim_y + 40][dim_x + 40];
    int (*map)[dim_x+40] = (int(*)[dim_x+40])&map_boundaries[20][20];
    In fact, 'map' points to 'map_boundaries' , map[0][0] is map_boundaries[20][20].
    I did what is posted in the second post here:
    Negative array indexing - Everything2.com

    I want 'map' to be global. Until now i had defined the dim_y and dim_x so that worked fine.
    Now i just need to read from a user the dim_x and dim_y.
    Until now i have global
    Code:
    int **map_boundaries;
    and then in main i use calloc:

    Code:
    map_boundaries = (int **)calloc(dim_y + 40,sizeof(int*));
    for(i = 0; i < dim_y + 40; i++){
        map_boundaries[i] = (int *)calloc(dim_x + 40,sizeof(int));
    }

    but i dont know how to declare this line now:
    Code:
     int (*map)[dim_x+40] = (int(*)[dim_x+40])&map_boundaries[20][20];

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Not possible, since your first code relies on the 2D array being contiguous in memory, and that cannot be achieved by multiple calloc() calls.


    The usual - and safer - technique is to map indices rather than beating the compiler into submission with type conversions (which is what your code is doing).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    3
    So how could i refer with map[0][0] to map_boundaries[20][20], with map[1][0] to map_boundaries[21][20], with map[-1][0] to map_boundaries[19][20] and so on...?

    I can do this:

    Code:
        scanf("%d",&dim_x);
        scanf("%d",&dim_y);
        
        int map_boundaries[dim_y][dim_x]
        
        (*map)[dim_x+40] = (int(*)[dim_x+40])&map_boundaries[20][20];
    but i need 'map' to be global, when i declare it as int **map i get this warning:
    assignment makes integer from pointer without a cast
    Last edited by george_engineer; 10-14-2014 at 06:34 AM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by george_engineer View Post
    So how could i refer with map[0][0] to map_boundaries[20][20], with map[1][0] to map_boundaries[21][20], with map[-1][0] to map_boundaries[19][20] and so on...?
    Why - specifically - do you need to do that?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    If I understand
    1. You have an (x, y) pair representing your map coordinates.
    2. The "user" want to treat x and y as values that can be negative, i.e. x in [-20, 20] and y in [-20, 20]
    3. C doesn't let you use negative array indices, so the program must map [-20, 20] to [0, 40].

    Then you need to do some simple mapping -- this usually involves just some basic arithmetic -- that takes a map coordinate and turns it into the corresponding index array. Use the "index value" to access the array.

    Perhaps make a list of each coordinate and it's corresponding array index. For example, for x
    Code:
    coord: -20 -19 -18 -17 -16 ...  -1   0   1 ...  18  19  20
    index:   0   1   2   3   4 ...  19  20  21 ...  38  39  40
    Now, what operation can you do to -20 that produces 0, that you can also to to -19 to get 1, ... and to 20 to get 40? Add, subtract, multiply or divide? By how much?

    When you figure out the actual mapping process, I highly recommend you make a small function (or two functions if you want to map x and y separately) to do that for you. Call that everywhere. That way, if you want to change your map coordinates or dimensions, you only need to change one or two small functions instead of a dozen or more different places.

  6. #6
    Registered User
    Join Date
    Oct 2014
    Posts
    3
    I have built a simulator and i need the boundaries for initialization, in case the user inserts wrong coordinates, he might give coordinates that are in the map, but around the coordinates some other coordinates have to be occupied, unfortunately it is to much to explain :/, but thanx for the answers
    Last edited by george_engineer; 10-14-2014 at 12:50 PM.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by george_engineer View Post
    I have built a simulator and i need the boundaries for initialization, in case the user inserts wrong coordinates, he might give coordinates that are in the map, but around the coordinates some other coordinates have to be occupied, unfortunately it is to much to explain :/, but thanx for the answers
    Allowing negative indices on an array does not address that problem. Doing error checking of the coordinates entered by the user, and correction as needed, will though.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-13-2013, 12:01 AM
  2. Replies: 8
    Last Post: 04-04-2012, 09:03 PM
  3. structure array calloc realloc help
    By BrandNew in forum C Programming
    Replies: 2
    Last Post: 03-08-2011, 09:04 AM
  4. Need help with allocating an array with calloc
    By MSF1981 in forum C Programming
    Replies: 2
    Last Post: 05-06-2009, 07:45 AM
  5. CALLOC / 2-D Array Creation
    By wbeasl in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 01:59 PM