简体   繁体   中英

C pass pointer to function, how to?

Having the following code, how can I have "get" receive a map, and return the value at the specified position?

I am attempting to write a cellular automaton in C to try to wrap my head around pointers and memory allocation. Everything was fine until I decided to make "get" to obtain data instead of a direct map[x+world.w*y] as I used to. I require this because in the future, I plan to have two maps of the same size, and use the same function to get data from them (so it'd be "get(&map2, x, y)" instead of "get(&map, x, y)".

I do this because I was advised against using globals, so I will keep the two maps in main and send their addresses to functions to process.

This is C language, so no C++ solutions are valid.

I tried to search for this in google but all documentation is extremely technical and convoluted, and I am not sure of how this procedure is actually named... So, can anyone help me with this? How can I pass a malloc'ed array to a function and retrieve or alter data from it?

typedef struct Map {
int HP;
int type;
unsigned int flags;
} Map;

typedef struct World {
int w;
int h;
} World;

struct World world;

int tile (int x, int y) { return x + world.w * y; }

int get (/*unknown*/map , int x, int y){
int val = x + world.w * y;
return /*unknown ->?*/ type;
}


int main (){
Map* map;
world.w = 8;
world.h = 8;
int tiles = world.w * world.h;
map = (Map*)malloc(sizeof(Map) * tiles);
int i;
for(i = 0; i < tiles; i++){
    map[i].type = rand()%2;
}
int x,y;

while(1){
    put(0,0);
    for(y = 0; y < world.h; y++){
        printf("\n");
        for(x = 0; x < world.w; x++){
            printf("%i ", get(&map, x, y));
        }
    }
};
printf("\n");
return 0;

}

Instead of:

get(&map, x, y)

in which you pass the address of the address of the map pointer that malloc() returned, just pass the address itself:

get(map, x, y)
AFAICT from your code, malloc( ) returns exactly the thing that get( ) is looking for, ie, a pointer to someplace in memory that has room for 64 tiles. So get( ) could look something like:
 int get( Map *map, int x, inty ) { int val = x + map->w * y; // map is a pointer to struct, not the struct itself return val; // get( ) returns an int, and it's in val } 

That might be closer to what you want.

-- pete

There are a few other errors in your code, too, but this might let the compiler get off the ground.

Your map variable in main() is already a Map * so don't create a double-indirect pointer out of it by appling & to it at the call site.

Also, int get(Map *m, int x, int y) { ... return map[...]; } int get(Map *m, int x, int y) { ... return map[...]; }

Don't cast the return value from malloc(3) .

I may be wrong but from reading your question, it doesn't sound like you are really looking for a function pointer, rather a pointer to a Map structure to pass to a function. Perhaps you want to return a pointer to a particular Map element as well. If this is the case it would look something like the following:

typedef struct Map {
int HP;
int type;
unsigned int flags;
} Map;

typedef struct World {
int w;
int h;
} World;

struct World world;

int tile (int x, int y) { return x + world.w * y; }

Map * get (Map *map , int x, int y){
return map[x + world.w * y];
}


int main (){
Map *map;
Map *m;
world.w = 8;
world.h = 8;
int tiles = world.w * world.h;
map = (Map*)malloc(sizeof(Map) * tiles);
int i;
for(i = 0; i < tiles; i++){
    map[i].type = rand()%2;
}
int x,y;

while(1){
    put(0,0);
    for(y = 0; y < world.h; y++){
        printf("\n");
        for(x = 0; x < world.w; x++){
            m = get(map, x, y); // map was already declared as a pointer
            printf("%d\n", m->HP);
            printf("%d\n", m->type);
            printf("%X\n", m->flags);
        }
    }
};
printf("\n");
return 0;

Passing a pointer to a struct (or union) in C is easy:

typedef struct Foo
{
  int a      ;
  char b[32] ;
} FOO ;

void do_something_with_a_foo( FOO* p )
{
  // using the arror operation '->' to deference a structure pointer
  int  m = p->a    ;
  char n = p->b[3] ;

  // using the * operator to deference a structure pointer
  int  x = (*p).a    ;
  char y = (*p).b[3] ;

  return ;
}

void pass_a_foo_pointer_to_function()
{
  FOO  stack_instance  = { 3 , "hello, world" , } ;
  FOO* heap_instance   = malloc( sizeof(FOO) ) ;

  heap_instance->a = 12 ;
  strcpy( heap_instance->b , "this, that and the other" ) ;

  do_something_with_a_foo( &stack_instance ) ;
  do_something_with_a_foo(  heap_instance  ) ;

  free( heap_instance) ;

  return ;
}

Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM