-2
char *e; 
while(*e!=0)
{
  if(isalnum(*e))
    printf("%c",*e);

  e++;
} 

But *e is a pointer, right ? So address should be printed, right ?

David Jones
  • 4,766
  • 3
  • 32
  • 45
  • 4
    That code contains basic syntax errors. Remember that C is *case-sensitive*. – T.J. Crowder Oct 22 '19 at 08:49
  • 4
    Mandatory reference to the [book list](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). Trying to learn C, especially if you don't already understand pointers, by looking at code and asking lots of questions is likely to be frustrating. – Useless Oct 22 '19 at 08:57

3 Answers3

4

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);
    }
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • `'\0'` may arguably be more correct, but `0` is idiomatic. – T.J. Crowder Oct 22 '19 at 09:08
  • @T.J.Crowder Since both are identical values of identical type (`int`) in C there’s an argument that `0` is just as correct as `\0` but, unlike you, I don’t think that `0` is more idiomatic. I definitely want to see `char` constants in code that’s performing work on characters. – Konrad Rudolph Oct 22 '19 at 09:12
  • @KonradRudolph - I defer to your almost-certainly greater experience. I always wrote `'\0'`. (Long time since I wrote C regularly.) But I've almost never seen it in others' code. :-) – T.J. Crowder Oct 22 '19 at 09:21
0

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.

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42