![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| compare array contents with file | orahi001 | UNIX for Dummies Questions & Answers | 0 | 03-25-2008 01:44 PM |
| A final question! Compare character with each array element | rorey_breaker | Shell Programming and Scripting | 1 | 09-28-2007 06:26 AM |
| Multidimension character array | janemary.a | High Level Programming | 1 | 05-15-2007 03:50 AM |
| Can I assign the contents of file into an array? | murtaza | Shell Programming and Scripting | 10 | 03-28-2007 12:58 AM |
| converting character string to hex string | axes | High Level Programming | 5 | 09-20-2006 10:04 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
converting contents of a character array to int
Hi,
I have character array and i need to convert the content to int. program snipet: char array[10] = {"1","2","3","4","5","6","7","8","9"}. I need to to extract each of these array fields and store them into an integer variable. Casting the contents to (int), gives the ascii value of the array fields but not the actual value. atoi function doesnt work here as it expects a const char*. Any help would be appreciated. Jyoti |
| Forum Sponsor | ||
|
|
|
|||
|
char datatype is treated as a limited range int (-127 to 127) by the compiler.
Code:
int i=0;
int irray[10]={0};
char array[10] = {'0','1','2','3','4','5','6','7','8','9'}; <- use ' not "
for(i=0;i<10;i++)
irray[i]=array[i];
|
|
||||
|
jim, i tried the code you've posted, its printing out the ascii values of the numbers (48, 49, and so on).
Won't this be as simple? Code:
#include<stdio.h>
int main(void) {
char a[]={'0','1','2','3','4','5','6','7','8','9'};
int b[10];
int i;
for(i=0;i<10;i++) {
b[i]=(int)(a[i]-48);
}
}
|