tags:

views:

144

answers:

5

I have a pointer ( *ascii ) which a pointer to a char and I want it is value as an int in order to make it an if statement like

if ( ascii == 32 || ((ascii > 96) && (ascii < 123)) {

}

This is not working, i would appreciate help

+7  A: 

Your code is not working because you are checking the value of the pointer (the memory address) not the value of the thing being pointed at. Remember a pointer is an address, you have to dereference it to get the value at that address.

Once dereferenced, you can simply do a comparison with a char type to those values:

 char ascii_char = *ascii;

 if ( ascii_char == 32 || ((ascii_char > 96) && (ascii_char < 123)) 
 {

 }
Doug T.
Beginner's guide: the pointer is your finger. The char is the moon. Do not confuse the two ;-)
Steve Jessop
+3  A: 

Just put a * before the variable name

if ( *ascii == 32 || ((*ascii > 96) && (*ascii < 123)) {

}

or just assign it to another variable and use it instead

int a = *ascii
if ( a == 32 || ((a > 96) && (a < 123)) {

}
klez
Thank you for posting an answer :)
c2009l123
A: 

I believe this will do the trick

int asciiNum = (int)*ascii;
ACBurk
A: 

If you are not referring to comparing just the first char but the number stored in that ascii pointer then use atoi() function to convert the char array to a number and then compare.

Murali VP
Isn't atoi non-standard !
nthrgeek
No it is part of standard C library libc
Murali VP
It's itoa which is non-standard. Also std::iota in the SGI STL is not in standard C++. The two are easily typoed.
Steve Jessop
+1  A: 

Why do you go for ASCII value?

Following would be more readable way:

char ascii_char = *ascii;

if ( ascii_char == ' ' || ((ascii_char >= 'a') && (ascii_char <= 'z'))

{

}

Jack