0

pretty much here is a rough outline of what i have done

struct monster
{
  int health;
  int armor;
  monster *nextaddr;
};

int main()
{
  int num;
  monster *pointer;

  printf("how many monsters do u want");
  sscanf(%d, &num);

  pointer = (monster*)malloc(num * sizeof(monster));
  
  //Here i need some kind of LOOP to populate "x" number of monster objects depending in the input the user entered, my problem arises because im not sure how to correctly do this with the pointer i am given....any advice is greatly appreciated!

  return 0;
}
2
Contributors
3
Replies
5
Views
6 Years
Discussion Span
Last Post by gerard4143
0

Here's what I would do in the for loop

pointer[0].health = some_value;
pointer[0].armor = some_value;
pointer[0].nextaddr = &pointer[1];

Instead of using the constants 0,1 for the array indexes, use variables that increment in the for loop. Also make sure you remember that the last node points to nothing(NULL).

Edited by gerard4143: n/a

0

thanks so much! im curious as to what would happen if i tried to do pointer[3].health = some_value;

when i only allocated enough space for 3 structures instead of 4? would my program crash? would it still work but overwrite code that might be used or unused??

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.