Thread: Feeding a 2d array into a function

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    21

    Feeding a 2d array into a function

    Hello,

    I am trying to get a function to perform a task on a 2d array, the array itself is already created outside of the function and merely needs to be passed to it so that it can perfom an action on it.

    For refrence the 2d array is a map (which can be created in different sizes) which has rooms in it. The function needs to perform an algorithm to connect corridors. I have been able to do this already but I now need to transfere it into a function.

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    you could receive it as simply a **array, with the dimensions passed as well.

    Something like:

    Code:
    int draw_map( char **map, unsigned int width, unsigned int height )
    {
    // drawing code here
    }
    You'll have bounds checking available if you have the dimensions.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    an alternative way that will compile without warnings and won't segfault is to pass the data in as if it were a 1D array, then compute the 2D index yourself.
    Code:
    void f(char *p,int rows,int cols) {
          int index;
    
          // index into 2D array
          // where the array is p[rows][cols]
          // index = (row * cols) + col
    
          // p[0][0]
          index  = 0 * cols + 0;
          // p[1][0]
          index = 1 * cols + 0;
          // p[0][1]
          index = 0 * cols + 1;
          // p[2][1]
          index = 2 * cols + 1
    }
    
    ...
    char *a[10][[10];
    f(a[0],10,10);

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    21
    But if I do this and feed in the array will it change the array I feed in, or do I have to return it and set the out of function array to equal the array returned by the function.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    21
    This is what I have done so far:

    Code:
    void zeroSight(int **sight, unsigned int rows, unsigned int columns){
    
        for(int i=0; i<columns; i++){ //initialise for loop to set seen to 0. This line go along y
        
            for(int j=0; j<rows; j++){ //on each y co-ordinate go along the x
            
                sight[j][i] = 1; //for each co-ordinate x and y set the seen value to 0 [or 1 to reveal map]
                
            }    
        }          
    }
    When I call the function in the main method:
    Code:
    zeroSight(mapSight, mapX, mapY );
    I get the error
    "cannot convert `int (*)[((unsigned int)((int)mapY))]' to `int**' for argument `1' to `void zeroSight(int**, int, int)' "

    Any idea what I am doing wrong?

    Thanks

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    What was "mapSight" declared as ?
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    21
    I've done a bit more work and have a bit more to show.

    First I have redeclared the array:

    Code:
        int **mapSight = (int **)malloc(mapX * sizeof(int *));
        
        for (int i = 0; i < mapX; ++i) {
            mapSight[i] = (int *)malloc(mapY * sizeof(int));
        }
    Edit: The original mapSight was just int mapSight[mapX][mapY];

    This has fixed some problems, however there is another array/function I am trying to do which are as follows:

    The array
    Code:
        char **mapChar = (char **)malloc(mapX * sizeof(char *));
        
        for (int i = 0; i < mapX; ++i) {
            mapChar[i] = (char *)malloc(mapY * sizeof(char));
        }
    The function
    Code:
    void mapFill(char **map, int rows, int columns, char cell){
    
        for(int i=0; i<columns; i++){ //initialise for loop to set seen to 0. This line go along y
        
            for(int j=0; j<rows; j++){ //on each y co-ordinate go along the x
            
                map[j][i] = cell; //for each co-ordinate x and y set the seen value to 0 [or 1 to reveal map]
                
            }    
        }          
    }
    And now I get a new error of:

    cannot convert `char**' to `int**' for argument `1' to `void mapFill(int**, int, int, char)'

    Edit 2: I now managed to get rid of that error and it compiles (I prototyped wrong) now it crashes. I'm not sure if it is creating the mapChar array properly
    Last edited by Sephmk; 03-13-2012 at 02:54 AM.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    another way:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef char X[3][3];
    
    void f(X x) {
            printf("%d\n",x[1][2]);
    }
    
    int main(int argc, char** argv)
    {
            X x;
            x[1][2] = 3;
            f(x);
    
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 35
    Last Post: 12-01-2011, 08:31 PM
  2. Replies: 17
    Last Post: 09-03-2011, 02:40 PM
  3. Passing Array To Function & Display Array Contents
    By mcertini in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2010, 01:32 PM
  4. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  5. Replies: 6
    Last Post: 11-09-2006, 03:28 AM