char *e;
while(*e!=0)
{
if(isalnum(*e))
printf("%c",*e);
e++;
}
But *e
is a pointer, right ?
So address should be printed, right ?
char *e;
while(*e!=0)
{
if(isalnum(*e))
printf("%c",*e);
e++;
}
But *e
is a pointer, right ?
So address should be printed, right ?
But*e is a pointer right ?
No. e
is a pointer. *e
is the first char
it points to. When used in an expression (rather than a type declaration) as a unary operator (an operator with one operand), *
is the indirection operator.
What does a *variable !=0 mean?
Assuming the loop is actually:
while(*e!=0)
...it means to keep looping while the char
that e
points to is !=0
. C strings are terminated with a 0 char
, '\0'
(but usually written as simply 0
).
I should note that the loop in the question, in addition to basic typos like Char
instead of char
and While
instead of while
, has the major logical error that it never changes the value of e
within the loop body. That means if it loops once, it'll loop forever; it needs ++e;
somewhere. (e
is also never initialized.) So for instance:
char *e = /*...something...*/;
while (*e != 0)
{
if(isalnum(*e)) {
printf("%c",*e);
}
++e;
}
or, really, this is what for
is for:
char *e;
for (e = /*...something...*/; *e != 0; ++e)
{
if(isalnum(*e)) {
printf("%c",*e);
}
}
For starters there are typos
Char *e;
^^^^
While(*e!=0)
^^^^
{
if(isalnum(*e))
printf("%c",*e);
}
You mean
char *e;
while(*e!=0)
{
if(isalnum(*e))
printf("%c",*e);
}
Moreover the pointer e
is not initialized. It must be initialized by a string. For example
char *e = "FaThima Hussain";
while(*e!=0)
{
if(isalnum(*e))
printf("%c",*e);
}
And at last the pointer within the loop is not being changed. A valid code snippet can look like
char *e = "FaThima Hussain";
while( *e!=0 )
{
if(isalnum(*e))
printf("%c",*e);
++e;
}
In the condition of the while loop there is a character pointed to by the pointer e is compared with zero that is in fact with the null terminating character. So it would be better to write
char *e = "FaThima Hussain";
while( *e != '\0' )
{
if(isalnum(*e))
printf("%c",*e);
++e;
}
Each string in C is includes and terminates with the zero-terminating character. So within the condition there is a check whether it is the end of the string. If not then enter in the body of the loop.
In the call of the function isalnum you have to cast the character to the type unsigned char as the function requires.
Here is a demonstrative program.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char *e = "FaThima Hussain";
while( *e != '\0' )
{
if ( isalnum( ( unsigned char )*e ) )
printf( "%c", *e );
++e;
}
putchar ( '\n' );
return 0;
}
The program output is
FaThimaHussain
That is the blank characters are not outputted because they are not alpha-numeric characters.
There is an old C rule: declaration resembles use. So if char *e
is a declaration of storage for pointer to a char
, *e
is use, .i.e. access to object pointed by e
.
Provided code also contains UB because e
is not initialized. Result of inspecting variable which wasn't initialized is undefined. Dereferencing such pointer would be undefined as well because by definition that pointer doesn't point at anything, and isn't a null pointer.