Possible Scope Problem

This is a discussion on Possible Scope Problem within the C Programming forums, part of the General Programming Boards category; Why am I having a problem with scope here. The record count is zero after the loop? BTW this is ...

  1. #1
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784

    Possible Scope Problem

    Why am I having a problem with scope here. The record count is zero after the loop? BTW this is a code slice. Also if I use a static integer I get a proper record count, but I'm not sure why I would have to bother with a static counter since the structure is defined outside of the loop.

    Code:
    	
    int main()
    {
    	FILE *fptr;
    	counter_t counter = {0,0,1,1,NULL};
    	strcpy( counter.date, GetSystemDate() );
    
    while( (buffer = GetRecord(fptr) ) != NULL)
    	{
    		counter.rec_count += 1;
    		//Break up COBOL datafile record for each salesman
    		GetCobolRecord(buffer,unedited_rec);
    		//build C style linked list out of extracted members
    		AddNode(&first,unedited_rec, key);
    	}
    	fclose(fptr); //close input file
    
    	printf("%d",counter.rec_count); exit(1);
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Very strange. The variable should retain it's value...can't see the innerworkings of the functions, but, again, the scope looks fine.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Is in your original code the file opened? In the code you've posted, you are using the filepointer and closing the file. But it's not opened.

    I guess the function GetRecord returns NULL imediately. A good method of debugging your program is putting printf's in the code.
    I suggest you put a printf inside the loop, if this printf is never printed, then GetRecord returns NULL imediately.

    Just another note. Don't use exit at the end of the main-function, but return. Some people use exit when something fatal has occurred. But it is always possible to avoid the use of exit, it's just like goto.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    How is counter_t declared?

  5. #5
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    How is counter_t declared?
    Code:
    typedef struct
    {
    	int rec_count;
                       //....
    }counter_t;
    Wait, there is nothing wrong with my code, the file pointer or anything like that. I know that it all works. It's just that I can't get this counter to work. Everything else works.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    > counter_t counter = {0,0,1,1,NULL};
    This suggests to me that .date is a char*, not a char []

    > strcpy( counter.date, GetSystemDate() );
    Which means this is simply broken in the absence of more information.

    To detect the possibility that it is a memory overrun problem, try this
    Code:
    typedef struct {
        char low_guard[10];
        // rest of structure members
        int rec_count;
        //....
        char high_guard[10];
    }counter_t;
    Initialise the guards to some pattern, and check it survives the loop.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There is not enough information in this code snippet to find out what is wrong. For example:

    What does 'GetRecord' do? Where does it get the record too?
    Where is 'unedited_rec' declared?

    You're obviously leaving something out here.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Actually Salem did figure out the problem. There is an overrun and indeed I had mistakenly used a char date[9] instead of a char *. After I used the high and low buffers I was getting the number of records. Just trying to track down the over run now.

    The reason why I accidently used char date[9] is because I've been making a lot of changes to this damn code!

  9. #9
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Okay, I found the over-run. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Binary Search Tree scope? problem
    By tms43 in forum C++ Programming
    Replies: 5
    Last Post: 11-01-2006, 09:13 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. Replies: 5
    Last Post: 11-07-2005, 10:34 PM

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