Thread: Unwelcome return value.

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    80

    Unwelcome return value.

    My question is why atoi returns a value of -954337623 or something like that in the following function.

    The fgets function reads from a textfile which holds 20 rows of 1s, 0s and newline chars.

    Code:
    int getmap(int map[20][50])
    {
    	int x = 0, y = 0;
    	char rdBuffer[50], c;
    	FILE* mapfile = fopen(MAP_NAME, "r");
    	if (mapfile == NULL)
    	{
    		printf("Could not open file properly");
    		return MAPERROR;
    	}
    	for (;y < 20; y++)
    	{
    		fgets(rdBuffer, 50, mapfile);
    		for (; x < 50; x++)
    		{
    			c = rdBuffer[x];
    			map[y][x] = atoi(&c); // Need help here, please!
    			if (map[y][x] != 1 && map[y][x] != 0)
    			{
    				return MAPERROR;
    			}
    			else
    			{
    				switch(map[y][x])
    				{
    					case 1: printf("#"); break;
    					case 0: printf(" "); break;
    					default: ;
    				}
    			}
    		}
    		printf("\n");
    		x = 0;
    	}
    	fclose(mapfile);
    	return 0;
    }

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Because you're very confused.

    You're declaring c as a char, and passing the address of it to atoi. But atoi is looking for a char array that is terminated with a null character. If it's just one character that you want to convert to a number, consider using rdBuffer[x]-'0'. atoi is suitable for strings like "514", not a single character.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    80
    what exactly does the -'0' do?

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    The numbers in ASCII are contiguous so by subtracting the value of '0' you turn a char into its integer representation.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    80
    Thank you both for replying, I'll look into this now and hopefully I will learn something...

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > for (;y < 20; y++)
    So explain exactly why you didn't do
    for (y = 0 ;y < 20; y++)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by Salem
    > for (;y < 20; y++)
    So explain exactly why you didn't do
    for (y = 0 ;y < 20; y++)
    He initialised y to zero on declaration.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  8. #8
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Odd... I thought I explained this in my reply to your earlier post.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    for (;y < 20; y++)
    This one is okay. But
    Code:
    for (; x < 50; x++)
    this one isn't, since it is itself in a loop.

    Change it to
    Code:
    for(x = 0; x < 50; x ++)
    dwk

    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: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Both are poor use of for loops.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Both are poor use of for loops.
    The ones that look like this
    Code:
    for (; x < 50; x++)
    are. But these
    Code:
    for(x = 0; x < 50; x ++)
    are okay.
    dwk

    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: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    Sometimes it is useful to have a for loop without an initialising statement. For instance, if you want to start doing something at the point where you left off something else previously, or if there is nothing you actually want to initialise there. In this case, it's probably better for readability to initialise y in the for loop rather than when it's declared, but that doesn't mean that leaving out the first statement is in general a poor use of for loops.

  13. #13
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Yes, it's just a choice of what is more readable and clean
    Code:
    for (; expr1; expr2)
    {
        statements
    }
    or
    Code:
    while (expr1)
    {
        statements
        expr2
    }

  14. #14
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by Stoned_Coder
    The numbers in ASCII are contiguous so by subtracting the value of '0' you turn a char into its integer representation.
    IIRC Prelude or Quzah said that the standard requires that the numbers be contiguous in a representation.

  15. #15
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by Thantos
    IIRC Prelude or Quzah said that the standard requires that the numbers be contiguous in a representation.
    Indeed, from ISO 9899:1999 5.2.1:

    "In both the source and execution basic character sets, the
    value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  3. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  4. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM