Note that there are some explanatory texts on larger screens.

plurals
  1. POValues of an array changing randomly during execution
    primarykey
    data
    text
    <p>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.</p> <p>Here is the fonction creating an array from the txt file, which works.</p> <pre><code>// 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 &amp;&amp; k&lt;SIZE_HEIGHT) /* read each line */ { int i; char *token = line; /* point to the beginning of the line */ for ( i = 0; i&lt;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=&amp;map; return p; </code></pre> <p>}</p> <p>Then I try to put the grid on the sreen (not the whole fonction):</p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #include &lt;SDL/SDL.h&gt; #include &lt;time.h&gt; #include &lt;string.h&gt; #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-&gt;format, 255, 255, 255)); int (*map)[SIZE_HEIGHT][SIZE_WIDTH]; map=newMap(); SDL_Rect positionFond; int i; int j; for(j=0; j&lt;SIZE_HEIGHT; j++) { for(i=0; i&lt;SIZE_WIDTH; i++) { positionFond.x = 100*i; positionFond.y = 100*j; if((*map)[j][i]+1) { SDL_BlitSurface(carreVert, NULL, ecran, &amp;positionFond); }else { SDL_BlitSurface(carreRouge, NULL, ecran, &amp;positionFond); } } } </code></pre> <p>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?</p> <p>Edit:no error an compiler.</p> <p>Edit: Any guess of what might do that would be gladly accepted.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload