Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > char array to int array

Reply
Thread Tools

char array to int array

 
 
rajus
Guest
Posts: n/a
 
      03-11-2006
How to convert a char array to an int array?So if

char s[]={"1234"};
then the int array say 'num' should have values num[0]= 1 num[1]=2 and
so on.

 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      03-11-2006
rajus schrieb:
> How to convert a char array to an int array?So if
>
> char s[]={"1234"};
> then the int array say 'num' should have values num[0]= 1 num[1]=2 and
> so on.


Allocate sufficient storage, say with
int *array = malloc(strlen(s) * sizeof *array);
if (array == NULL) {
/* Your error handling here; in its absence: */
exit(EXIT_FAILURE);
}
Then iterate through s, make sure that you have a digit,
e.g. by using isdigit(); if you find a non-digit, terminate,
if the non-digit is not '\0', emit an error message;
whenever you have a digit, convert this digit to the
digit's value (e.g. using "digit - '0'") and store it in
the appropriate element of array.

If your implementation does not work, post your compiling
code here and explain your problem as clear as possible.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
 
 
 
Neil
Guest
Posts: n/a
 
      03-12-2006
rajus wrote:
> How to convert a char array to an int array?So if
>
> char s[]={"1234"};
> then the int array say 'num' should have values num[0]= 1 num[1]=2 and
> so on.
>

a loop and atoi()
 
Reply With Quote
 
Jim Smith
Guest
Posts: n/a
 
      03-12-2006
Neil <(E-Mail Removed)> writes:

> rajus wrote:
>> How to convert a char array to an int array?So if
>> char s[]={"1234"};
>> then the int array say 'num' should have values num[0]= 1 num[1]=2 and
>> so on.
>>

> a loop and atoi()


No.
 
Reply With Quote
 
rajus
Guest
Posts: n/a
 
      03-12-2006
Thanks! The digit-' 0' is a great way to convert the char to int and
also the dynamic allocation.
Thanks.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-12-2006
"rajus" <(E-Mail Removed)> writes:
> Thanks! The digit-' 0' is a great way to convert the char to int and
> also the dynamic allocation.
> Thanks.


Please read <http://cfaj.freeshell.org/google/>.

I'm not sure whether this has been mentioned in this thread, but the
digit-'0' trick is guaranteed to work because the C standard requires
the digit characters '0' through '9' to be consecutive and ordered.
Keep in mind that there is no such guarantee for letters (and there
are character sets in use where the letters are not consecutive).

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
int urldecode(char *src, char *last, char *dest) gert C Programming 20 02-16-2007 11:28 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
newbie: char* int and char *int trey C Programming 7 09-10-2003 03:24 AM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments