Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4015436
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:37:08+00:00 2026-05-20T09:37:08+00:00

Having the following code, how can I have get receive a map, and return

  • 0

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;

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:37:09+00:00Added an answer on May 20, 2026 at 9:37 am

    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, i.e., 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: SELECT <column>, count(*) FROM <table> GROUP BY <column> HAVING
I'm having an issue with the following code: private void DataPortal_Fetch(TaskCriteria criteria) { using
I'm having some problems with the following code: private class ClientPluginLoader : MarshalByRefObject {
Ok. I'm having an issue with the following bit of code: StreamReader arrComputer =
Imagine having the following scenario: You need to create a system where the Back
I'm having the following problem. I created a application context file for spring.net. The
I'm having trouble getting the following to work in SQL Server 2k, but it
I'm having trouble trying to optimize the following query for sql server 2005. Does
I'm having problem in the following line: rd.PrintOptions.PaperSize = PaperSize.PaperFanfoldStdGerman; it throws an exception
I'm having a little problem with the following: When I execute this line: echo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.