www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/11/05/13:39:55

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
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 <stdio.h>
#include <stdlib.h>
#include <assert.h>

#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<index; ++j)	printf ( "%s\n", (char*)KeyWords[ j ] );
	return;
}

------- end code ----------
____________________________________________________
"The most loving parents and relatives commit murder with
smiles on their faces. They force us to destroy the person
we really are: a subtle kind of murder."
                - James Douglas Morrison
    
   
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: 2.6.2
mQCNAzGH2REAAAEEAJhDdEd5O7fzWZ02wVzUDY/lWsB6MYquLNdnbXzt+9hMA3AH
o+eQY/9tyJ2O7bhmVOmHyqU2Fo5I0BruEsVuakSLplsUI4nxjOvS2ftlCoq8I+re
SjsghWhL1/bfJWLiSk2RxgD+QbUICpA4vg0+4CywnB11UYJ+RSWFM1/KTNf1AAUR
tCNOLiBNYXR0aGV3IE1pbGxlciA8bmFtaWxsZTJAdnQuZWR1Pg==
=e9Pi
-----END PGP PUBLIC KEY BLOCK-----

- Raw text -


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