From: namille2 AT vt DOT edu (Matthew Miller) Newsgroups: comp.lang.c,comp.os.msdos.djgpp Subject: array of pointers to strings, BC++, DJGPP and weird printing behavior Date: 5 Nov 1996 17:02:52 GMT Organization: VPI Lines: 103 Message-ID: <55nrvs$sdo@solaris.cc.vt.edu> NNTP-Posting-Host: as2511-5.sl004.cns.vt.edu Mime-Version: 1.0 Content-Type: Text/Plain; charset=US-ASCII To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Hello, I have got the array of pointers to strings concept down, but now (I think) something weird is happening. This code snippet (along w/ a main function) compiles without errors on both BC++ 3.1 and DJGPP. But, the executables don't act the same. With DJGPP, running the exe. produces an endless loop of seg. faults. Also, the file that is opened is just a plain text file containing one word per line While, when compiled w/ BC++ the program executes fine, but, if you look a the source code there are two statements to print the array of strings; one is inside the do while loop (there is comment above it) and another right after the loop. This is where the odd behavior comes in, printing the strings from within the loop goes fine, no problem. While, outside the loop when the strings are printed there is a few garbage characters tagged at the end of the string. Why is there this difference? The same statements are used to print the strings, but the output is different depending if printed from inside the loop or outside. Path: <- printed from inside the loop Path:  <- printed from outside the loop Can someone help with this, exp. help me to get this code to work with DJGPP. Thanks. Matthew Miller -------- the below is the offending code --------- #include #include #include #define MAX_KWLENGTH 40 /* THE MAX. LENGTH OF THE KEYWORD */ #define INI_NAME "ngformat.ini" /* NAME OF THE KEYWORD FILE */ void ReadIni ( void ) { char **KeyWords; int index = 0, WordNumber = 0, j; FILE *ini; char string[ MAX_KWLENGTH ], *ret, *key[ MAX_KWLENGTH ]; /* ALLOCAT THE FIRST POINTER TO THE KEYWORD STRING */ KeyWords = (char **) malloc ( sizeof(char) ); assert ( KeyWords != NULL ); ini = fopen ( INI_NAME, "r" ); if ( ini == NULL ) { fprintf ( stderr, "\nReadIni Error!! -- Unable to open %s.\n", INI_NAME ); exit ( EXIT_FAILURE ); } do { ret = fgets ( string, MAX_KWLENGTH, ini ); if ( ret != NULL ) { if ( WordNumber != 0 ) { /* ALLOCAT THE NEXT POINTER TO THE KEYWORD STRING */ KeyWords = (char **) realloc ( KeyWords, sizeof(char) ); assert ( *KeyWords != NULL ); ++WordNumber; } *key = (char*) strtok ( string, "\n" ); /* STRIP NEWLINE */ KeyWords[ index ] = (char *) malloc ( strlen(*key)*sizeof(char) ); assert( KeyWords[ index ] != NULL ); strcpy( (char*) KeyWords[index], *key ); /* First print statement */ printf ( "%s\n", (char*)KeyWords[ index ] ); ++index; } } while ( ret != NULL ); /* Second print statement */ for (j=0; j