5 Replies - 21747 Views - Last Post: 06 June 2008 - 08:25 AM Rate Topic: -----

#1 baconbeastnz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 29-May 08

Cast Char[] to int

Posted 05 June 2008 - 08:57 PM

I am getting strange numbers when outputing my attempt to convert this:
e.g 6 comes out as : -872412618

basically i can successfully convert each element in the array to an int, but i cant convert the whole char array to an int

Heres my C code:

char enterred[100];  
fgets ( enterred, 100, stdin );  
theNoUnsigned = (unsigned char)theNO; // theNo : each elemtn in the forloop, works good!
theValue = (int )enterred;
printf("\n\n int value: %d \n", thevalue);



** Edit ** :code:

This post has been edited by baconbeastnz: 05 June 2008 - 09:17 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Cast Char[] to int

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Cast Char[] to int

Posted 05 June 2008 - 09:18 PM

This kind of cast does not work. The reason is that the data type char is also a numeric type! That is right, characters are numbers. Everything in the computer is numbers. The characters are codes (for example ASCII or UTF8) that represent letters and symbols.

To convert a character string like "3141" to a number requires that you parse the number from the string.

Now you can do it as I did in the snippet, or you can use one of several built in function:

sscanf() this can be very useful, especially when parsing a line with multiple values in it.

atoi() -- parses a single integer out of a string.
Was This Post Helpful? 0
  • +
  • -

#3 baconbeastnz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 29-May 08

Re: Cast Char[] to int

Posted 05 June 2008 - 09:41 PM

Thanks for your reply, have used the atoi() function, which worked brilliantly!

However, I am a little confused by your explanation. I can convert each element in the array to an int by casting it as a char, which converts the ASCII representation into decimal value. (whether its a number or a character)

So surely the reason I cannot cast the array, is more to do with the n.o of elements in the array. The cast function can only deal with one paramater, where as atoi() can deal with several paramaters ( each element in the array ) ?
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Cast Char[] to int

Posted 05 June 2008 - 09:58 PM

well technically I was wrong in my explanation. What you actually did was cast a char * into an int. the number you got was an offset in memory to the array "enterred".

So you are correct, If you were to cast enterred[0] to an int you would get the character code for the first character. Note that a character and and int are of different sizes. You can't "cast" the whole array into an int (the int could not hold a number 100 bytes long... unless most of them were 0 and in the right order).
Was This Post Helpful? 0
  • +
  • -

#5 baconbeastnz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 29-May 08

Re: Cast Char[] to int

Posted 06 June 2008 - 01:46 AM

ok cool thanks for confirming that.

I have switched a to a GCC compiler (after using VC9) and am getting new compile problems. E.G:

char vowelList[7];
vowelList[0] = "a"; //error here, error: invalid conversion from `const char*' to `char'|

Why is my vowelList[] array const? I havent declared it as such.
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Cast Char[] to int

Posted 06 June 2008 - 08:25 AM

The problem is very subtle. You used the wrong quotation marks.

"A" - is a c-string thus it is a const char *
'A' - is a character thus it is a char.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1