delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2002/05/17/12:17:16

From: "Matthew Bayliss" <noone AT knowhere DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Static variables
Date: Fri, 17 May 2002 17:03:03 +0100
Lines: 202
Message-ID: <ac39ir$m9gr2$1@ID-106847.news.dfncis.de>
NNTP-Posting-Host: 195.44.51.226
X-Trace: fu-berlin.de 1021651355 23380834 195.44.51.226 (16 [106847])
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.3018.1300
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.3018.1300
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

I have a variable declared outside of main() with a static keyword.

If you break on line 117 and "print words_ptr" the pointer has an
assignment.  When the function returns to main() (line 31) the variable has
no value.

Why is this?  I thought a static variable would also keep it's value between
funtions.

Help.

Matt

=============================================

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define WORDSARRAYSIZE 50
#define WORDSFILENAME "..\\words.txt"
#define PHRASESFILENAME "..\\phrases.txt"

// Structure of data file will be "ENGLISH,KOREAN".
static int words_elements = 0;
struct wordstruct{
 char english[WORDSARRAYSIZE];
 char korean[WORDSARRAYSIZE];
};

static struct wordstruct *words_ptr;

int inputdata( struct wordstruct *words_ptr ); // Function to read words
from a file.

void extractenglish( char tempstring[] , char tempenglish[] );
void extractkorean( char tempstring[] , char tempkorean[] );

int main( void )
{

 if ( inputdata( words_ptr ) == 0 ) // Read the data in.
 {
  // process the rest of the program.
  int c;

  for ( c = 0; c < words_elements; c++ )
  {
   printf( "%s\n", words_ptr[words_elements].english );
  }

 }
 else
 {
  // display a file error.
  printf("Error reading data!\n");
 }


 return 0;

}


// ***************************************************************
//
// Returns 0 on success, non-zero if an error occured.
//
// ***************************************************************
int inputdata( struct wordstruct *words_ptr )
{
 int err = 0;  // Initialize the error code, only change it if an error
occurs.
 FILE *fp;

 if ( ( words_ptr = malloc( sizeof( struct wordstruct ) ) ) !=NULL )
 {
  // memory allocated
  words_elements = 0;

  if ( ( fp = fopen( WORDSFILENAME, "r" ) ) != NULL )
  {
   // Read the data.
   char tempstring[2*WORDSARRAYSIZE];
   char tempenglish[WORDSARRAYSIZE];
   char tempkorean[WORDSARRAYSIZE];
   int loopcount = 0;

   while ( ( fgets( tempstring, 2*WORDSARRAYSIZE, fp ) ) != NULL )


    loopcount++;
    /*

    Some psuedo-code:

    if ( numelements >= 0 )
     copy array[numelements] = data;
     numelements++;

    */

    extractenglish( tempstring , tempenglish );
    extractkorean( tempstring , tempkorean );

    if ( words_elements == 0 )
    {
     strcpy ( words_ptr->english, tempenglish );
     strcpy ( words_ptr->korean, tempkorean );
     words_elements++;
    }
    else if (words_elements > 0 )
    {
     struct wordstruct *temp_ptr = NULL;

     // Make more room
     temp_ptr = realloc( words_ptr, ((WORDSARRAYSIZE*2) * words_elements) +
(WORDSARRAYSIZE * 2) );
     words_ptr = temp_ptr;

     // copy data
     strcpy( words_ptr[words_elements].english , tempenglish );
     strcpy( words_ptr[words_elements].korean , tempkorean );

     // increment counter.
     words_ptr++;

    }


    printf("The english for %s is %s.\n", tempkorean, tempenglish );


   }

   fclose( fp );
   fp = NULL;

  }
  else
  {
   // There was an error, report it.
   err = 1;
  }
 }
 else
 {
  // Memory allocation error
  err = 1;
 }




 return err;

}

void extractenglish( char tempstring[] , char tempenglish[] )
{
 int length = 0;

 while ( tempstring[length] != ',' )
  length++;

 strncpy ( tempenglish, tempstring, length );

 if ( length < WORDSARRAYSIZE )
  tempenglish[length] = '\0';
 else
  tempenglish[WORDSARRAYSIZE-1] = '\0';



}

void extractkorean( char tempstring[] , char tempkorean[] )
{
 char *start_p = NULL;
 int count = 0;

 // Find the , and shift along one.
 start_p = strpbrk( tempstring, "," );

 start_p = start_p + 1;

 // HEREIN LIES A BUG: if \n does not come before EOF then it will gpf.
 while ( *start_p != '\n' )
  tempkorean[count++] = *start_p++;

 if ( count < WORDSARRAYSIZE )
  tempkorean[count] = '\0';
 else
  tempkorean[WORDSARRAYSIZE-1] = '\0';



}




- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019