简体   繁体   English

*variable?=0 是什么意思?

[英]What does a *variable !=0 mean?

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

  e++;
} 

But *e is a pointer, right?但是*e是一个指针,对吧? So address should be printed, right?所以地址应该打印,对吧?

But*e is a pointer right?但是*e 是指针对吗?

No. e is a pointer.编号e是一个指针。 *e is the first char it points to. *e是它指向的第一个char 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? *variable?=0 是什么意思?

Assuming the loop is actually:假设循环实际上是:

while(*e!=0)

...it means to keep looping while the char that e points to is !=0 . ...这意味着在e指向的char!=0时继续循环。 C strings are terminated with a 0 char , '\0' (but usually written as simply 0 ). C 字符串以 0 char '\0'结尾(但通常写为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.我应该注意到问题中的循环,除了基本的拼写错误,比如Char而不是charWhile而不是while ,还有一个主要的逻辑错误,即它永远不会改变循环体内e的值。 That means if it loops once, it'll loop forever;这意味着如果它循环一次,它将永远循环; it needs ++e;它需要++e; somewhere.某处。 ( e is also never initialized.) So for instance: e也从未初始化。)例如:

char *e = /*...something...*/;
while (*e != 0)
{
    if(isalnum(*e)) {
        printf("%c",*e);
    }
    ++e;
}

or, really, this is what for is for:或者,真的,这就是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.此外,指针e未初始化。 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.在 while 循环的条件下,将指针 e 指向的字符与零进行比较,该字符实际上是 null 终止字符。 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. C 中的每个字符串都包含并以零终止字符结尾。 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.在调用 function isalnum 时,您必须按照 function 的要求将字符转换为 unsigned char 类型。

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程序 output 是

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 .有一条旧的 C 规则:声明类似于 use So if char *e is a declaration of storage for pointer to a char , *e is use, .ie access to object pointed by e .因此,如果char *e是指向char的指针的存储声明,则使用*e ,即访问e指向的 object 。

Provided code also contains UB because e is not initialized.提供的代码还包含 UB,因为e未初始化。 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.取消引用此类指针也将是未定义的,因为根据定义,该指针不指向任何东西,也不是 null 指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM