Thread: Help with Debugging

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    2

    Help with Debugging

    I just started writing in C. So any help is appreciated. This is a program to find all the pairs of numbers that add to give the inputted number

    Code:
    // Main Function
    void _main(int a1)
    {
    	
    
    	// Find All Numbers where int a + int b = a1
    
    	
    		
    		// Start by finding the size of array needed
    		// Odd integers have (a-1)/2 values
    		// Even have a/2 values
    
    		if ( ( a1%2 ) == 1 ) 
    		{
    		     int adders = ((a1 - 1) / 2);
    		}
    		else 
    		{
    		     int adders = (a1 / 2);	
    		}
    		
    	
    
    		// Iniate array and store values
    	
    		int adderarray[adders][2];
    			
    		
    		for (int i;i <= adders; i++)
    		{
    		     adderarray[(i-1)][0] = i;
    					
    		     adderarray[(i-1)][1] = (a1 - i);
    		}
    				
    
    }
    I get error:

    'adders' undeclared (first use in this function).

    So when the array 'adderarray' is declared the variable adders doesn't exsist, right? But it should have a value from the If/else statement?

    I'm trying to figure out why it is wrong not just what is wrong. Thanks for helping.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Variables have scope. To figure out the lifetime of a variable, find its declaration, move backward to the first open-curly, and then find the matching close-curly. That matching close-curly is the end of the road for that variable. So your first variable adders ONLY exists in the "then" part of the if statement, and the second variable adders ONLY exists in the "else" part. By the time we get below, both adders variables have died.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    2
    Changed to
    Code:
    if ( ( a1&#37;2 ) == 1 ) 
         int adders;
    		{
    		     adders = ((a1 - 1) / 2);
    		}
    		else 
    		{
    		     adders = (a1 / 2);	
    		}

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That is only valid in C99 - C89 would complain about declaring variables in that place.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Main must return int, not void.
    Main shall not be defined as _main, but rather main.
    And lastly, main shall take no arguments or it shall take
    int main(int, char**)
    So says the standard.

    Adders should be defined before the if statement.
    Also remember that the way you create your array is also C99.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dev-C++: Problems with Breakpoint Debugging
    By Thileepan_Bala in forum C Programming
    Replies: 1
    Last Post: 01-17-2008, 10:48 AM
  2. Problem in debugging in Eclipse
    By Bargi in forum Linux Programming
    Replies: 1
    Last Post: 08-21-2007, 09:53 AM
  3. Debugging Dev C++
    By tuurb046 in forum Tech Board
    Replies: 10
    Last Post: 08-16-2007, 12:51 PM
  4. Debugging book recommendation
    By dagans in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 09-13-2005, 07:35 PM
  5. debugging directx apps
    By confuted in forum C++ Programming
    Replies: 1
    Last Post: 08-16-2003, 08:56 AM