global variable turns zero

This is a discussion on global variable turns zero within the C Programming forums, part of the General Programming Boards category; I have a Windows console application. And I have declared few global variable. Found while debugging, this global( i.e. RunFlag) ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    175

    global variable turns zero

    I have a Windows console application. And I have declared few global variable.
    Found while debugging, this global( i.e. RunFlag) variable becomes 0 while execution sscanf function.

    Code:
    BYTE RunFlag;
    
    if ( sscanf( p+4,"%02X%03X",&Id,&frequency) != 2 )
    where frequency is global variable and Id is local.

    Can anybody help and tell why this weird this happening

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Can you post a bit more code (a small, compilable section that shows the problem)

    If RunFlag is being changes indirectly, it's probably due to an overflow somewhere (at a guess).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    If its not to hard, try rewriting the code without the use of global variables. You can pass locals with the const modifier to make sure they are not changed. Const is not supported on all systems though, so watch out.
    The keyboard is the standard device used to cause computer errors!

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    Code:
    BYTE RunFlag;
    INT16 frequency;
    
    BYTE foo(BYTE *p)
    {
    	BYTE Id,error = 0;
    	
    	// Get the ID and frequency from command string
    if ( sscanf( p+4,"%02X%03X",&Id,&frequency) != 2 )  RunFlag is origionally1 
    //and turn 0 here
    	{
    		ERROR(error = 10);
    		return error;
    	}
    where frequency and RunFlag is global variables and Id is local.

    Do you think that it is problem with sscanf function.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I don't see anything obviously wrong there. More debugging, I'm afraid

    What is ERROR ?

    Try to pinpoint the exact location the variable changes. Add a load of :
    >printf ("RunFlag %d\n", RunFlag);
    statements to the code

    I you still can't get it, break the code down into smaller chunks and go from there.

    If you want help, post something on here that we can compile and run.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    Thanks for everybody

    Salems's post helped me to resolve the peoblem.

    But I want to clarify one thing, do you means that all pointer that I pass to sscanf must point to 32 bit number.

    As I am developing this application in VC++, I looked as MSDN and example is as follow

    Code:
    #include <stdio.h>
    
    void main( void )
    {
       char  tokenstring[] = "15 12 14...";
       char  s[81];
       char  c;
       int   i;
       float fp;
    
       /* Input various data from tokenstring: */
       sscanf( tokenstring, "%s", s );
       sscanf( tokenstring, "%c", &c );
       sscanf( tokenstring, "%d", &i );
       sscanf( tokenstring, "%f", &fp );
    
       /* Output the data read */
       printf( "String    = %s\n", s );
       printf( "Character = %c\n", c );
       printf( "Integer:  = %d\n", i );
       printf( "Real:     = %f\n", fp );
    }
    this does not require content of pointer to be 32 bit. If that is the case, don't you think is limitation of sscanf?

    please let me know

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global variable and prototype
    By Lincoln_Poh in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2008, 04:25 AM
  2. Global variable in shared library
    By krock923 in forum C Programming
    Replies: 5
    Last Post: 01-11-2008, 04:56 PM
  3. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 04:31 PM
  4. Global variable???
    By loopy in forum C Programming
    Replies: 13
    Last Post: 06-09-2002, 04:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21