Domanda

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.

È stato utile?

Soluzione

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.

Altri suggerimenti

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.

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;
}

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top