home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 18750 < prev    next >
Encoding:
Internet Message Format  |  1992-12-21  |  1.6 KB

  1. Path: sparky!uunet!noc.near.net!ceylon!news.tufts.edu!jade.tufts.edu!slahiri
  2. From: slahiri@jade.tufts.edu (Sandip Lahiri)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Why won't this work?? (scanf(), etc.)
  5. Message-ID: <1992Dec21.182407.14093@news.tufts.edu>
  6. Date: 21 Dec 92 18:24:07 GMT
  7. References: <JVIGNEAU.92Dec10205719@cs.ulowell.edu>
  8. Sender: news@news.tufts.edu (USENET News System)
  9. Distribution: comp
  10. Organization: Tufts University - Medford, MA
  11. Lines: 32
  12.  
  13. In article <JVIGNEAU.92Dec10205719@cs.ulowell.edu> jvigneau@cs.ulowell.edu (|Joe Vigneau|) writes:
  14. >This time I just can't figure it out... I am trying to build a binary tree,
  15. >with structures as nodes. This isn't the problem (yet...). The problem is that
  16. >I can't get scanf to work! This is the first time in the program that any sort
  17. >of input is called for, if there's a buffer problem or somethinf like that.
  18. >BTW: I'm using Ultrix and gcc.
  19. >
  20. >----trouble lies just ahead---------
  21. >struct Tree *starttree(void)
  22. >{   struct Tree *topnode;
  23. >    char *last, *first, *mid;
  24. >
  25. >    topnode = (struct Tree *)malloc(sizeof(struct Tree));
  26. >
  27. >    if (topnode == NULL){
  28. >    puts("starttree: malloc returned NULL");
  29. >    exit(1);
  30. >    }
  31. >
  32. >    printf("First Name          : "); scanf("%s",first);
  33. >/*                           ---------^
  34. > * The problem's right here.-| When the program gets this far, it dies and
  35. > * says "Bus error (core dumped)" I've tried using gets(), but that didn't
  36. > * work either. This is the problem.
  37.  
  38. Allocate memory for first first. So the following would do,
  39.    char first[100];
  40. Now, scanf("%s", first); 
  41. will work. But my personal opinion is *not* to use scanf. Use fgets.
  42. Hope this helps.
  43.  
  44. -Sandip
  45.