From: rca@csd4.csd.uwm.edu (Robert C Allender)
Subject: MY problem? ...or Borland's? (also on comp.lang.c)
Originator: rca@csd4.csd.uwm.edu
Organization: Computing Services Division, University of Wisconsin - Milwaukee
Date: Sat, 25 Jul 1992 16:38:52 GMT

I'm VERY new to C, but I can usually hold my own by R(ing)TFM.  This is 
different.  I've checked through Borland's FAX document catalog (I use 
Borland's C++ 3.0, but I use the standard C mode), ALL the manuals, a few 
3rd party books on Borland C ... nothing.  Here's the problem:
I initialize a char * in main() and pass it to a function that gets a string, 
passing the address of the string back to main().  If I try to print the 
string back at main(), it works.  I pass the char * to the next function, and
I can print the string at the beginning of that function, but by the end of 
it, the string is garbage.  If I then pass the value to main() and send it to
yet aother function, the array is empty.  I can put checks in anywhere and
find the strlen() to be correct at ANY of the points where it won't print, 
but the values get corrupt and then dissappear.  What am I doing wrong?

Here's the boiled-down code...

#include <everything that's needed>
#define LENGTH 10

main_bkgrd();
main_title();
main_entry();
get_info();
char * scrollbox(char,int,int);
next(char *);
prev(char *);
make_box(int,int);
char * get_dir(char,int,int);
char get_drive();
clear_half(char *);
list_dir(char *);

main() {
  char drive, *c;
  char dummy;

  clrscr();
  main_bkgrd();
  main_title();
  main_entry();
  make_box(61,10);

  do {
    do {
      drive=get_drive();
    } while (drive == NULL);
    c = scrollbox(drive,61,10);  <---- This gets a directory, etc. 
  } while(!c);
  
  clear_half(c);                 <-- can print the array here BEFORE the end
				     of the function, else it's garbage.
  list_dir(c);                   <-- by this point the array values are GONE.

  scanf("%c",&dummy);            <-- Just holds the screen so I can see 
  return (0);                        where it messed up and what it's doing.
}

list_dir(char *dir)  {           <-- "c" from main()
  char seekdir[100];             <-- define new array so that "\"'s can be 
				     replaced with reqired "\\"'s  in 
				     _dos_findfirst().
  struct find_t z;                                
  int count,end;                 <-- generic counters
  int counta, countb;

  textcolor(3);
  for (count=1; count<strlen(dir); count++)    <-- try printing the OLD array
    cprintf("%c",dir[count]);                       for kicks (doesn't work)
  
  textcolor(0);
  textbackground(1);
  for(counta=1, countb=1; countb<strlen(dir); counta++, countb++) {
     seekdir[counta] = dir[countb];
     if (dir[countb] == '\\') {          <-- all this just sets up the NEW
	counta++;                            array for processing by 
	seekdir[counta] = '\\';              _dos_findfirst().
     }
  }
  end = _dos_findfirst(seekdir,_A_NORMAL,&z);
  while (!end)  {
     cprintf(" %s\n",z.name);
     end = _dos_findnext(&z);
  }
  return(0);
}


clear_half(char *p) {
  int row, counter, max;
  textbackground(0);
  textcolor(7);
  max=strlen(p);

  textbackground(1);
  textcolor(7);
  gotoxy(4,12);
  cprintf("Directory of: ");
  for (counter=1; counter<max; counter++)     <-- Print the array.  It WORKS
    cprintf("%c",p[counter]);                     at this point!

  textbackground(0);
  for(row=14; row<24; row++)  {
    gotoxy(5,row);
    cprintf("                                 <-- Just blanks out a line 
  }                                               (didn't all fit on the page)

  textbackground(1);
  textcolor(7);
  for(row=13; row<23; row++)  {
    gotoxy(4,row);
    cprintf("                                                                          ");
  }
  return(0);
}

If anyone knows how to fix this, I'd really appreciate the help.  If you can
E-Mail me (rca@csd4.csd.uwm.edu), I'll post a summary of all the reasonable
solutions.

Thanks

--------------------------------------------------------------------------
| Robert C. Allender    |  "Exactly!" said Deep Thought. "So once you do | 
| rca@csd4.csd.uwm.edu  |  know what the question actually is, you'll    | 
| rca@bfs.uwm.edu       |  know what the answer means."                  | 
|                       |        -The Hitchhiker's Guide to the Galaxy   | 
--------------------------------------------------------------------------
-- 




Newsgroups: comp.os.msdos.programmer,comp.os.msdos.desqview