| View previous topic :: View next topic |
| Author |
Message |
Odecey Master Cheater
Reputation: 1
Joined: 19 Apr 2007 Posts: 259 Location: Scandinavia
|
Posted: Wed Jun 18, 2008 11:11 am Post subject: [Help] Converting in C++ |
|
|
How do i convert a char type to an int? Ive tried this:
| Code: |
char someChar[20] = "6";
int someInt = atoi(&someChar);
|
but it gives this error:
| Code: |
.\dllmain.cpp(633) : error C2664: 'atoi' : cannot convert parameter 1 from 'char (*)[20]' to 'const char *'
|
|
|
| Back to top |
|
 |
Symbol I'm a spammer
Reputation: 0
Joined: 18 Apr 2007 Posts: 5094 Location: Israel.
|
Posted: Wed Jun 18, 2008 11:49 am Post subject: |
|
|
someChar is already a pointer to someChar[0], try:
| Code: | | atoi(&someChar[0]); |
or
| Code: | | char* someChar = "6"; |
|
|
| Back to top |
|
 |
Odecey Master Cheater
Reputation: 1
Joined: 19 Apr 2007 Posts: 259 Location: Scandinavia
|
Posted: Wed Jun 18, 2008 12:11 pm Post subject: |
|
|
| The second suggestion you gave still gives me the same error, and it also conflicts with other code in my program. Also wont the first suggestion only convert the first char in someChar?
|
|
| Back to top |
|
 |
Odecey Master Cheater
Reputation: 1
Joined: 19 Apr 2007 Posts: 259 Location: Scandinavia
|
Posted: Wed Jun 18, 2008 12:41 pm Post subject: |
|
|
How come? There are several functions just for this in C#, why wouldnt they have implemented it for C++ aswell? (Talking about .ToString, int.parse, etc).
_________________
Never confuse activity with productivity. You can be busy without a purpose, but what's the point?- Rick Warren |
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Wed Jun 18, 2008 1:52 pm Post subject: |
|
|
| Code: | char aChar[2] = "6";
int nInt = atoi(aChar); |
??
_________________
|
|
| Back to top |
|
 |
Dark Byte Site Admin
Reputation: 475
Joined: 09 May 2003 Posts: 25975 Location: The netherlands
|
Posted: Wed Jun 18, 2008 2:05 pm Post subject: |
|
|
| Quote: |
| Code: |
atoi(&someChar[0]);
|
|
Both are valid solutions, and do the same
atoi requires a pointer to the first character of the string and will parse it till the end (0 byte).
&someChar[0] will get from the first character in the array the address to it
This works, but isn't really required, since aChar on itself is also just an pointer that points to the first character of the array
_________________
Tools give you results. Knowledge gives you control.
Like my help? Join me on Patreon so i can keep helping |
|
| Back to top |
|
 |
STN I post too much
Reputation: 43
Joined: 09 Nov 2005 Posts: 2673
|
Posted: Thu Jun 19, 2008 4:49 am Post subject: Re: [Help] Converting in C++ |
|
|
| Odecey wrote: | How do i convert a char type to an int? Ive tried this:
| Code: |
char someChar[20] = "6";
int someInt = atoi(&someChar);
|
but it gives this error:
| Code: |
.\dllmain.cpp(633) : error C2664: 'atoi' : cannot convert parameter 1 from 'char (*)[20]' to 'const char *'
|
|
i would point out the error.
| Quote: | char someChar[20] = "6";
int someInt = atoi(&someChar); |
the character marked with bold is the culprit. Like DB said, someChar already points to the first character, by using &(address of) operator, you are passing the address of address of someChar(or first character). Removing the & will solve the problem.
int someInt = atoi(someChar);
I hope this example will make it clear.
| Code: | char someChar[20] = "6";
int someInt = atoi(someChar);
cout << "passing someChar: " << someChar << endl;
cout << "passing &someChar: "<< &someChar; |
outputs
passing someChar: 6
passing &someChar: 0012FF6C
0012FF6C is a random memory address allocated for the string someChar, could be different on your pc.
You should read more about strings aka character arrays.
http://ee.hawaii.edu/~tep/EE160/Book/chap7/subsection2.1.1.2.html
_________________
|
|
| Back to top |
|
 |
|