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

  • Home
  • SEARCH
  • 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 6702699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:05:03+00:00 2026-05-26T07:05:03+00:00

I’m trying to make a little programme in C using SDL, displaying a robot

  • 0

I’m trying to make a little programme in C using SDL, displaying a robot moving on a grid. This grid is represented by a txt file of 0s and 1s.

Here is the fonction creating an array from the txt file, which works.

// create a map(array) from a text file
int (*newMap())[SIZE_HEIGHT][SIZE_WIDTH]
{
static const char filename[] = "input.txt";  /* the name of a file to open */
FILE *file = fopen(filename, "r");  /* try to open the file */
int map[SIZE_HEIGHT][SIZE_WIDTH];

char line[BUFSIZ]; /* space to read a line into */
int k = 0;
while ( fgets(line, sizeof line, file)!=NULL && k<SIZE_HEIGHT) /* read each line */
{
    int i;
    char *token = line; /* point to the beginning of the line */

    for ( i = 0; i<SIZE_WIDTH; i++ )
    {
        map[k][i]=((int)*token)-48;
        token+=sizeof(char);
        printf("map[%d][%d]=%d\n", (int)k,(int)i,map[k][i]);
    }
    puts("----\n");
    k++;
}
fclose(file);

int (*p)[SIZE_HEIGHT][SIZE_WIDTH];
p=&map;
return p;

}

Then I try to put the grid on the sreen (not the whole fonction):

#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <time.h>
#include <string.h>
#include "Parameters.h"
#include "simulation.h"
#include "editor.h"

void simulate(SDL_Surface *ecran)
{
SDL_Surface *carreVert = SDL_LoadBMP("carreVert.bmp");
SDL_Surface *carreRouge = SDL_LoadBMP("carreRouge.bmp");
SDL_Surface *robot = SDL_LoadBMP("robotRouge.bmp");

SDL_SetColorKey(robot, SDL_SRCCOLORKEY, SDL_MapRGB(robot->format, 255, 255, 255));

int (*map)[SIZE_HEIGHT][SIZE_WIDTH];
 map=newMap();
SDL_Rect positionFond;

int i;
int j;

for(j=0; j<SIZE_HEIGHT; j++)
{
    for(i=0; i<SIZE_WIDTH; i++)

    {


        positionFond.x = 100*i;
        positionFond.y = 100*j;
        if((*map)[j][i]+1)
        {

            SDL_BlitSurface(carreVert, NULL, ecran, &positionFond);
        }else
        {


            SDL_BlitSurface(carreRouge, NULL, ecran, &positionFond);
        }

    }

}

And then something strange happens: when I observe the array *map with the debugger, I see that the values are changing when I go through the test. So the grid does appear, but not with the right pattern. Why does that happen?

Edit:no error an compiler.

Edit: Any guess of what might do that would be gladly accepted.

  • 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-26T07:05:04+00:00Added an answer on May 26, 2026 at 7:05 am

    The array map is local to the function. It ceases to exist when the function finishes. You pass its address to the caller, but when the caller tries to use it … BANG!

    Fast solution: make the array map a static one: make sure you never call newMap more than once per run.

    Other solutions:

    • move the creation of the array outwards and pass the address around
    • use malloc and friends to manage the array
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

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.