View Single Post
  #1   (View Single Post)  
Old 17th April 2011
xmorg xmorg is offline
Real Name: Tim Cooper
Fdisk Soldier
 
Join Date: Sep 2010
Location: San Diego
Posts: 56
Default C: address of an int in an array in a struct?

This is an SDL program. gr->rcFlora[1] is an SDLRect with members x,y,w,h
Previously gr->rcFlora[1] was just a normal SDLRect that I declared on top of the code block. Now that I moved it into the struct "gr", it causes the program to segfault.

I allocate memory for it in this way

gr = (GameRes *) malloc(sizeof(GameRes));
LoadResources(gr, scrmode);


Inside the LoadResouces Function I can access the variables like so

Code:
printf("Loading broken tower.\n");
  temp = IMG_Load("data/broken_tower.png");
  gr->flora[1] = SDL_DisplayFormatAlpha(temp);
  gr->rcFlora[1].x = 600; gr->rcFlora[1].y = 128;
  printf("Is the value accessible here?-> x= %d\n",gr->rcFlora[1].x); //Here they work but not in main?
  SDL_FreeSurface(temp);
Code:
if(gr->rcFlora[1].x >= 0 && gr->rcFlora[1].y >= 0 ) {
	printf("SDL_BlitSurface(gr->tower, NULL, screen, &gr->rcFlora[1]);\n"); //This prints
      	SDL_BlitSurface(gr->tower, NULL, screen, &gr->rcFlora[1]); //it crashes here
	printf("success!\n"); //This does not print.
I hope it makes sense. My question is why cant I access the address of an int in an array which is a member of a struct?
Reply With Quote