Go Down

Topic: Converting a char into an int (Read 682 times) previous topic - next topic



I'm trying to convert the content of a char array (made of numbers) into an int (or at least an array of integers); something like passing data from char serialNum[n] to int calculateNum[n].

The atoi() function from the stdlib library seems like it could do the trick but i havn't been able to use it properly so far.



What do you mean by a character array?

Do you mean

char serialNum[] = { '1', '2', '3', '4', '5' }


char serialNum[] = "123456";

If you mean something like the first, then that would be the source of the problem with the atoi function.  This function expects to receive a 'c' language string which has a '\0' as the last character of the array.  If you were to look at the memory map for the second version you would see this terminating null.

value = atoi(serialNum)  should work for the second definition of serialNum.

If the first definition was modified to:

char serialNum[] = { '1', '2', '3', '4', '5', '\0' }

It would work also.


This is great.
I meant  char serialNum[] = { '1', '2', '3', '4', '5' }.
Putting '\0' manually at the end of the array did it, i just added
serialNum[5] = '\0'
and the atoi function now runs properly.
Thank you very much!

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131