-
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??
-
how much physical memory fo you have?
how much data is in the arrays?
-
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?
-
Interesting....
Can you post some code?
-
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);
}
-
Check the return value of the sscanf function. If sscanf failes the value of the arrays may be undefined (except when declared static).
-
D'oh!
The most logic explaination....