Issue with atoi() on char array

https://codebender.cc/sketch:126698

On line 160 of the above code I attempt to convert the char array s[] to an int. Try as I might however i cannot seem to get it to work. The serial print on 163/164 pops out 0. If i change it to print s[0] or s[1] it prints the digits perfectly, so s[] is definitely getting filled correctly.

I know it's something stupid I'm doing but after a couple of days of research I cannot see the issue with my logic/syntax

Anyone able to help?

Regards

change

		char s[] = {0, 0};
		char m[] = {0, 0};
		char h[] = {0, 0};

to

		char s[] = { 0, 0, 0};
		char m[] = {0, 0, 0};
		char h[] = {0, 0, 0};

atoi() converts a C string, so you need to give it a C string.

What he's getting at, is your char arrays need an extra byte for the null terminator :wink:

Arh.... this is the issue with learning multiple languages at once. I thought the fact I had not specified a length when declaring the arrays meant a \0 was automatically added.

Shall go try the fix, thank you guys.

By not specifying a length, it allocated the memory based from what you initialise it to. You initialised it to 2 bytes, which allows 1 character and a null terminator. You needed space for 2 characters and a null terminator.

Thank for the help chaps, but still getting the same. I've tried

char s[] = {0, 0, 0};

along with

char[3];

Making sure in the second instance to input 2 digits so neither of s[0] or s[1] have garbage values in them. Still no luck.

You make a string but not with ASCII numbers. I see: s[0] = key; and s[0] = key; But an ASCII '0' is 0x30. So why do it the hard way of making is "ASCII" but simply create the right time in the switch? I mean, the input is clearly not serial. So the first 2 cases become:

switch (ctr)
				{
					case 0:
						if (key == 14)
							key = 0;
						time = key; //put it straight in there
						ctr ++;
						lcd.clear();
						lcd.print("Secs:");
						p = key;
						lcd.print(p);
						break;
					case 1:
						if (key == 14)
							key = 0;
						time = key * 10; //you want is to be the ten digit so just multiply
						ctr ++;
						lcd.clear();
						lcd.print("Secs:");
						lcd.print(p);
						p = key;
						lcd.print(p);
						break;

Rest is up to you.

So why do it the hard way

Because I was being a moron. First time i've picked this up since last week, I can remember trying to figure it out in each case and directly putting it into "time" but couldn't work the math in my head. Obviously too tired.

Just given it another go and got it in one. You are right, should have avoided the whole situation by directly assigning. I am still curious what I was doing wrong regarding atoi mind. At the beginning of the year I was building c-based trie and hash table data structures, stupid that taking a few off to learn html, php and css leaves me unable to operate the simple stuff ! :smiley:

Either way, it is now working, had to change a few things around further down the code but tis all for the better :smiley:

Thank you

https://codebender.cc/sketch:126826

R3BORNUK:
I am still curious what I was doing wrong regarding atoi mind.

You were putting 0x00, 0x01 etc into the char array. But atoi() expects the ASCII char for '0', '1' etc which are 0x30, 0x31 etc :wink: