Thread: Not understanding where this value is coming from... Help?

  1. #1
    Registered User
    Join Date
    May 2016

    Post Not understanding where this value is coming from... Help?

    int main(){
         int *map[12][12][5], monster = 0, mnstMdfy = 0,mnstDths[26];
         mnstDths[6] = 0;     
         map[1][0][0] = (int *)malloc(sizeof(int));
         map[1][0][0] = 71;
         printf("\nMap = %d\n",map[1][0][0]);
         monster = map[1][0][0] - 65;
         printf("\nMonster = %d\n",monster);
         mnstMdfy = mnstDths[monster] * 5;
         printf("\nModifier = %d\n",mnstMdfy);
    This is a rough outline of the actual code, but the given code should be sufficient. After running the program, the print statements have given me results such as...

    "Map = 71

    Monster = -189

    Modifier = 335"

    I've looked over this many times and cant seem to identify where the values of Monster and Modifier come from. They should be zero, but obviously that's not the case. Any help is appreciated!

    EDIT: I have found that splitting the monster assignment into...
    monster = map[1][0][0];
    monster = monster - 65;
    ... fixes this problem, however I still am not sure as to why this occurs. (Maybe the 65 is changing the address?) Please let me know!
    Last edited by Caleb Duncan; 08-05-2016 at 02:52 AM. Reason: Found a fix

  2. #2
    Registered User
    Join Date
    Feb 2012
    The program is giving lot of warnings. If you are trying to print the addresses then you need to use the %p specifier.

  3. #3
    Registered User talahin's Avatar
    Join Date
    Feb 2015
    My compiler says:
    gcc -Wextra -Wall -pedantic -std=c99 -o monsters monsters.c
    monsters.c: In function ‘main’:
    monsters.c:11:19: warning: assignment makes pointer from integer without a cast [enabled by default]
          map[1][0][0] = 71;
    monsters.c:13:6: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
          printf("\nMap = %d\n",map[1][0][0]);
    monsters.c:15:14: warning: assignment makes integer from pointer without a cast [enabled by default]
          monster = map[1][0][0] - 65;
    I think your biggest problem is that you are mixing integers and pointers to integers.
    If your compiler didn't warn you about that, maybe your first step should be increasing the warning level of your compiler.


  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    When you do a dynamic memory allocation,
         map[1][0][0] = (int *)malloc(sizeof(int));
    if you want to then use that memory location, you must dereference the pointer. What you're doing instead is just assigning a new pointer value (71, which is invalid) here:
         map[1][0][0] = 71;
    If I change your code to use *map[1][0][0] every time you want to use the value of the memory location, it works.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help coming up with an answer
    By burnsidex in forum C Programming
    Replies: 2
    Last Post: 09-02-2013, 09:38 PM
  2. my second coming
    By thestien in forum Game Programming
    Replies: 6
    Last Post: 12-17-2007, 09:54 AM
  3. what's the world coming to?
    By Waldo2k2 in forum A Brief History of
    Replies: 18
    Last Post: 11-22-2002, 12:00 PM
  4. What is the world coming to?
    By Eibro in forum A Brief History of
    Replies: 12
    Last Post: 09-04-2002, 01:57 PM

Tags for this Thread