home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!abarnett.demon.co.uk
- From: Ade The Shade <adrian@abarnett.demon.co.uk>
- Newsgroups: comp.sys.amiga.programmer
- Subject: Re: Need help with strings in SAS/C
- Date: Wed, 14 Feb 96 21:00:48 GMT
- Organization: BorderLine
- Message-ID: <9602142100.AA003wk@abarnett.demon.co.uk>
- References: <4fehq5$keg@news.unicomp.net>
- X-NNTP-Posting-Host: abarnett.demon.co.uk
- X-Newsreader: TIN [version 1.2 PL2]
- X-Mail2News-Path: disperse.demon.co.uk!post.demon.co.uk!abarnett.demon.co.uk
-
- Ray Schmalzl (rays@conline.com) spake thus:
- : Could somebody out there help me with strings in SAS/C? I have a few
- : books on C in general, but none of them cover this quite enough. I have two
- : problems. The first is getting the function atoi to work right. For example:
-
-
- : #include <stdio.h>
- : #include <stdlib.h>
- : void main()
- : {
- : char CharOne, CharTwo[]="012345";
-
- : CharOne = getch(); // assume typing in a digit here
-
- : printf("%c\t",CharOne) ;
- : printf("%i\n",atoi(CharOne)) ;
-
- : printf("%c\t",CharTwo[3]) ;
- : printf("%i\n",atoi (CharTwo[3]) ;
- : }
-
-
- : gives me an error message. the only way i can get it to work is to
- : declare a third variable as in the following:
-
-
- : #include <stdio.h>
- : #include <stdlib.h>
- : void main()
- : {
- : char CharOne, CharTwo[]="012345";
- : char CharThree[2]="";
- :
- : CharOne = getch(); // assume typing in a digit here
-
- : printf("%c\t",CharOne) ;
- : CharThree[0]=CharOne;
- : printf("%i\n",atoi(CharOne)) ;
- :
- : printf("%c\t",CharTwo[3]) ;
- : CharThree[0]=CharTwo[3];
- : printf("%i\n",atoi (CharTwo[3]) ;
- : }
-
-
- : Is there any way to do what I'm trying to do without declaring that extra
- : variable? And what is the deal with that "ecpecting char const * found
- : char" error?
-
- The problem is that CharOne is simply a single character, or just a byte.
- In C, a string MUST be terminated with a NULL character - "\0".
- This why you could do
- printf("%s \n", CharTwo );
-
- but not
- printf("%s \n", CharOne );
-
- You have to use "%c".
-
- One way round it would be to declare
- char CharOne[2] = "";
-
- and do
- CharOne[0] = getch();
-
- although as getch returns an int, not a char (according to my manual), you
- should really do
- CharOne[0] = (char)getch();
-
- Hope this helps.
-
-
- --
- __ __/__ . __ __ | Pope Adrian IV of the Church of The Holy Lungfish,
- (_/(_// / (_// / | Larry the Thrice-blessed. BAAWA ha ha.
-
- "A slug in mucus wins few friends."
-
-
-
-
-