5
void setup_map (int *map); <--- prototype

int row, col;  <-- global variables

some main 
{
  //get number of rows and cols from user
  int map[rows][cols]; //create map here because another function uses it
  setup_map (map[row][col]);
}

void setup_map (int map[row][col])
{
  loop through and set up map
}

my problem is I cant get the prototype quite right I was hoping somewhere could explain to me what my prototype needs to be? I have just begun learning about pointers and understand the concept pretty well just haven't ever used a 2d array as a argument. Thanks for any help.

D_Man
  • 67
  • 1
  • 1
  • 6
  • possible duplicate of [passing 2d arrays](http://stackoverflow.com/questions/3911400/passing-2d-arrays) – P.P Feb 26 '13 at 19:57
  • The link to passing 2d array had an answer that really helped me. It turns out I needed to use malloc to dynamically allocate my array and be able to pass it to a function. Thanks everyone for the help! – D_Man Feb 26 '13 at 20:24

4 Answers4

4

Correct prototypes include:

void setup_map(int map[ROWS][COLS]);
void setup_map(int map[][COLS]);
void setup_map(int (*map)[COLS]);

And to call it:

setup_map(map);

Note that, however, that the number of rows and columns needs to be a compile-time constant for this to work.

  • +1 for including valid prototypes but OP wants calling statement as well. Adding that will complete your answer – exexzian Feb 26 '13 at 20:00
  • So is there a way I could still get the amount of rows and cols from the user? Make a pointer to the 2d array and pass the pointer maybe? – D_Man Feb 26 '13 at 20:00
  • @user2041767 You will have to use dynamic memory allocation for that. –  Feb 26 '13 at 20:01
1

There are several things wrong.

First thing wrong, you are calling your function in the wrong manner.

You should not call setup_map (map[row][col]); instead you should call setup_map (map); because the array name is a pointer, array name plus [] operator fetches the contents of a particular place on the memory.

Then you need to make your prototype and your definition look the same, because the compiler will use the prototype (not the definition, if it is later) to parse if your code is correct, and will conclude things wrong if your prototype is wrong.

Now on the prototype itself:

If you REALLY need your function to expect a fixed size array, then you must use compile time constants.

Like this:

void setup_map( int map[23][9]);

or this:

#define ROWS = 23;
#define COLS = 9;
void setup_map( int map[ROWS][COLS] );

Remember that the name of the array, is a pointer.

speeder
  • 6,197
  • 5
  • 34
  • 51
1

heres an example

#include <stdio.h>

void print_matrix(int x, int y, int matrix[][x])
{
    int i, j;

    for (i = 0; i < y; ++i) {
        for (j = 0; j < x; ++j) {
            printf("[%i]", matrix[i][j]);
        }
        putchar('\n');
    }
}

int main()
{
    int matrix[][2] = {{0, 1}, {2, 3}};
    print_matrix(2, 2, matrix);
    return 0;
}
Dmytro
  • 5,068
  • 4
  • 39
  • 50
1

you have to give integer values while declearing an array in the way you do.

such as:

int map[5][10];

if you would like to give these values as row and col, first you have to define macros for row and cols.

macros:

#define ROW 5
#define COL 10

declaration in main():

int map[ROW][COL];

if you are planning to take row and col values from the user, things may get a little complicated because then you have to do dynamic memory allocation by using malloc() and free() functions.

int main()
{
    int *map;
    int row, col;

    printf("enter row and col value:");
    scanf("%d %d", &row, &col);

    map = (int *) malloc(row * col * sizeof(int));

    //use the array

    free(map);
    return 0;
}

example function prototype:

void setMap(int *map, int row, int col);

while doing function implementation use your function prototype. just add curly brackets and start to fill in.