home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 4147 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: mail2news.demon.co.uk!abarnett.demon.co.uk
  2. From: Ade The Shade <adrian@abarnett.demon.co.uk>
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: Need help with strings in SAS/C
  5. Date: Wed, 14 Feb 96 21:00:48 GMT
  6. Organization: BorderLine
  7. Message-ID: <9602142100.AA003wk@abarnett.demon.co.uk>
  8. References: <4fehq5$keg@news.unicomp.net>
  9. X-NNTP-Posting-Host: abarnett.demon.co.uk
  10. X-Newsreader: TIN [version 1.2 PL2]
  11. X-Mail2News-Path: disperse.demon.co.uk!post.demon.co.uk!abarnett.demon.co.uk
  12.  
  13. Ray Schmalzl (rays@conline.com) spake thus:
  14. :     Could somebody out there help me with strings in SAS/C? I have a few 
  15. : books on C in general, but none of them cover this quite enough. I have two 
  16. : problems. The first is getting the function atoi to work right. For example:
  17.  
  18.  
  19. : #include <stdio.h>
  20. : #include <stdlib.h>
  21. : void main()
  22. : {
  23. :    char CharOne, CharTwo[]="012345";
  24.  
  25. :    CharOne = getch();        // assume typing in a digit here
  26.  
  27. :    printf("%c\t",CharOne) ;
  28. :    printf("%i\n",atoi(CharOne)) ;
  29.  
  30. :    printf("%c\t",CharTwo[3]) ;
  31. :    printf("%i\n",atoi (CharTwo[3]) ;
  32. : }
  33.  
  34.  
  35. : gives me an error message. the only way i can get it to work is to 
  36. : declare a third variable as in the following:
  37.  
  38.  
  39. : #include <stdio.h>
  40. : #include <stdlib.h>
  41. : void main()
  42. : {
  43. :    char CharOne, CharTwo[]="012345";
  44. :    char CharThree[2]="";
  45. :    
  46. :    CharOne = getch();        // assume typing in a digit here
  47.  
  48. :    printf("%c\t",CharOne) ;
  49. :    CharThree[0]=CharOne;
  50. :    printf("%i\n",atoi(CharOne)) ;
  51. :    
  52. :    printf("%c\t",CharTwo[3]) ;
  53. :    CharThree[0]=CharTwo[3];
  54. :    printf("%i\n",atoi (CharTwo[3]) ;
  55. : }
  56.  
  57.  
  58. : Is there any way to do what I'm trying to do without declaring that extra
  59. : variable? And what is the deal with that "ecpecting char const * found 
  60. : char" error?
  61.  
  62. The problem is that CharOne is simply a single character, or just a byte.
  63. In C, a string MUST be terminated with a NULL character - "\0".
  64. This why you could do
  65. printf("%s \n", CharTwo );
  66.  
  67. but not
  68. printf("%s \n", CharOne );
  69.  
  70. You have to use "%c".
  71.  
  72. One way round it would be to declare
  73. char CharOne[2] = "";
  74.  
  75. and do
  76. CharOne[0] = getch();
  77.  
  78. although as getch returns an int, not a char (according to my manual), you
  79. should really do
  80. CharOne[0] = (char)getch();
  81.  
  82. Hope this helps.
  83.  
  84.  
  85. --
  86.  __ __/__ . __ __   | Pope Adrian IV of the Church of The Holy Lungfish,
  87. (_/(_//  / (_// /   | Larry the Thrice-blessed. BAAWA ha ha.
  88.  
  89. "A slug in mucus wins few friends."
  90.  
  91.  
  92.  
  93.  
  94.