简体   繁体   English

在c中使用字符串指针

[英]using pointers for string in c

So I wrote this code 所以我写了这段代码

char *word = "Metal Gear";
int counter = 0;
printf("word = ");

for(;;)
{
     printf("%c", *word);
     *(word++);
     counter++;
     if(*word == '\0')
        break;
}

The variable counter to count the length of my word string. 变量计数器来计算我的单词字符串的长度。
How come this code does not print the null character? 这段代码为什么不打印空字符?
I know that I told it to break once it encounters a null character, but my print statement is before the if clause, so shouldn't it print it then break out of the loop. 我知道我告诉它一旦遇到空字符就中断,但是我的print语句在if子句之前,所以它不应该打印它然后中断循环。
How come there's no difference if the if clause is at the very beginning of the for loop ?! 如果if子句位于for循环的最开始,怎么会没有区别呢?

update: while I'm at it, does c allow variable length for arrays or not? 更新:当我在它,C是否允许数组的可变长度?
I'm confused by this because I read that it allows it and that it doesn't. 我对此感到困惑,因为我读到它允许并且不允许。
My assumption that c99 allows it but many articles and blogs haven't been updated since the release of c99 standard. 我的假设是c99允许这样做,但是自c99标准发布以来,许多文章和博客都没有更新。 Am I correct? 我对么?

How come this code does not print the null character? 这段代码为什么不打印空字符?

Because you break the loop before it is printed. 因为您在打印循环之前就将其中断。 Also, a null character likely does not have any visual representation, so even if you print it, you would probably see nothing. 另外,空字符可能没有任何视觉表示,因此即使您打印它,也可能什么也看不到。

my print statement is before the if clause, so shouldn't it print it then break out of the loop. 我的打印语句在if子句之前,因此不应该打印它,然后跳出循环。

You increase the pointer by 1 after printing, so when determining when to break, you always check the next character after the one you just printed. 在打印后将指针增加1,因此在确定中断时间时,始终检查刚打印的字符之后的下一个字符。

First thing: printf expects a valid (ie non-NULL) pointer for its %c argument so passing it a NULL is officially undefined and is smart 第一件事:printf的%c参数期望一个有效的(即非NULL)指针,因此将NULL传递给它是正式未定义的并且很聪明

Second : According to logic You break from for loop before printing. 第二:根据逻辑,您在打印之前从for循环中退出。 If you want to check try following. 如果要检查,请尝试以下操作。

 #include <stdio.h>
 #include <string.h>

 int main()
  {
       char *word = "Metal Gear";
       int counter = 0;
       printf("word = ");

    for(;;)
     {
      printf("%c ", *word);
      if(*word == '\0'){
          printf("%d", counter);
          break;
       }
        *(word++);
        counter++;
     }
      return 0;
   }

As in printf, i have given space extra it will print as "word = M etal G ear 10" with 2 space gap between 'r' & '10' 如在printf中一样,我给了额外的空间,它将打印为“ word = M Getal G ear 10” ,在“ r”和“ 10”之间有2个空格

while I'm at it, does c allow variable length for arrays or not? 当我在使用它时,c是否允许数组的长度可变? I'm confused by this because I read that it allows it and that it doesn't. 我对此感到困惑,因为我读到它允许并且不允许。 My assumption that c99 allows it but many articles and blogs haven't been updated since the release of c99 standard. 我的假设是c99允许这样做,但是自c99标准发布以来,许多文章和博客都没有更新。 Am I correct? 我对么?

VLAs were introduced with C99. C99引入了VLA。 Sites that say C allows them are correct; 使用C的网站允许他们是正确的; sites that say C does not allow VLAs are out of date. 说C不允许VLA的网站已过时。

However, there are still C compilers/implementations in use that target C89 (C90, C94) (eg: gcc -std=c89 -pedantic ... ). 但是,仍在使用针对C89(C90,C94)的C编译器/实现(例如: gcc -std=c89 -pedantic ... )。 For these compilers, VLAs are not an option. 对于这些编译器,不能选择VLA。

Anyway, except for very small arrays, you should not use VLAs in your code :) 无论如何, 除了非常小的数组,您不应该在代码中使用VLA :)
Mainly because VLAs have no error checking. 主要是因为VLA没有错误检查。

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

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