problem with static char

This is a discussion on problem with static char within the C Programming forums, part of the General Programming Boards category; I have a program which calls a function. In this function four arrays are declared, all are static chars. If ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    329

    Question problem with static char

    I have a program which calls a function. In this function four arrays are declared, all are static chars.
    If i remove the static, and just declare them as char, the computer reboots, or halts..
    I'm running Dos 6.22, can anyone explain to me what's happening??

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    how much physical memory fo you have?

    how much data is in the arrays?

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    static char achBuf[100], achVName[21], achDesc[80], achVal[31];
    These arrays are just in the startup of the program, and there's enough memory.
    Why doesn't it crash when the vars are declared as static?

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Interesting....

    Can you post some code?

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    The program works fine, as long as i declare the chars in readIni as static.
    The way i've understood the static keyword in this situation, is that the value is retained after you leave it's scope, hence it's some kind of global variable.
    Am i wrong here?

    Code:
    int main( int argc, char *argv[]){
    
      //init some vars, call some other functions...
    
      if(readIni("Inifilename")){
        exit(1);
      }
    }
    
    int readIni(char *fName){
      static char achBuf[100], achVName[21], desc[80], achVal[31];
      FILE *fi;
      int i;
    
      if (access(fName, 4) == -1 || (fi = fopen(fName, "r")) == NULL){
        perror("inifile");
        return(-1);
      }
    
      while (fgets(achBuf,100,fi) != NULL){
        sscanf(achBuf,"%[^:]: %[^;]; %[^\n]", achVName, achVal, desc);
        if (strcmp(achVName,"PARAMETER1")==0){
          chEtterbeh = achVal[0];
          continue;
        }
      }
      fclose(fi);
    
      return(0);
    }

  6. #6
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Check the return value of the sscanf function. If sscanf failes the value of the arrays may be undefined (except when declared static).

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    D'oh!
    The most logic explaination....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 01:06 PM
  3. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 06:48 PM

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