473,394 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

char array to int array

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.

Mar 11 '06 #1
5 4800
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.
Mar 11 '06 #2
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()
Mar 12 '06 #3
Neil <Ne*******@worldnet.att.net> 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.
Mar 12 '06 #4
Thanks! The digit-' 0' is a great way to convert the char to int and
also the dynamic allocation.
Thanks.

Mar 12 '06 #5
"rajus" <ra****@gmail.com> 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) ks***@mib.org <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.
Mar 12 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Ying Yang | last post by:
Hi, whats the difference between: char* a = new char; char* b = new char; char c ;
21
by: Bret | last post by:
I'm curious why char** argv is acceptable in the main() declaration. In the comp.lang.c FAQ (question 6.18) it says that pointers to pointers and pointers to an array are not interchangable. ...
19
by: gaga | last post by:
I can't seem to get this to work: #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *names; char **np;
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
12
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the...
13
by: chellappa | last post by:
hi , please explain me , char pointer , char Array, char ,string... i have some many confussion about this... please clarify to me.. i need some example about this program by chellappa.ns
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
5
by: Stephen Cawood | last post by:
I'm trying to use a C++ .lib from C# (I tried the Interop group will no results). I have a working wrapper DLL (I can get back simple things like int), but I'm having issues dealing with an array...
2
by: Michael | last post by:
Hi, How to understand the difference between the following three. My understanding is the number in bracket minus one is the max number of chars to store in the char array , right? ...
3
by: ZMan | last post by:
The following code won't compile with gcc version 3.4.2 (mingw-special). How come? Error: cannot convert `char (*)' to `char**' /**********************************************************/...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.