[C language] char array to int array

December 26, 2009 at 01:34:27
Specs: Windows XP
i've got a variable from struct:
struct record {
char bmonth[20];
char bdate[20];
}list[100];

they stored numbers ranging 1-31
i would like to convert them to int array so i can manipulate them. it's urgent. any one? :(

i use Dev-C++ btw.

much thankss.


See More: [C language] char array to int array

Report •


#1
December 26, 2009 at 04:06:45
Have you tried starting the exercise yourself yet? What have you
got so far, and where did you get stuck?

Report •

#2
December 26, 2009 at 04:50:14
hey i got it:DD
by using atoi

int ibmonth[20];

for(i=0;i<k;i++)
{
ibmonth[i] = atoi(list[i].bmonth);
}

thanks bro.


Report •

#3
December 26, 2009 at 11:55:35
The bad thing about atoi is it returns 0 on an error; however, 0 may be a valid result in your data set. In your program, 0 would gum up the works. :P

You *might* consider using sscanf instead since it will return the number of successful conversions.

char s[] = "1234";
int num;

if((sscanf(s, "%d", &num)) != 1) {
   // an error occured
}

Either function will bail on the first non digit value though.


Report •

Related Solutions


Ask Question