我正在尝试使用指针打印字符串的每四个字符。
我能够使用以下代码实现它:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char const *argv[]){
char* string = "WhatTheeHell";
char* p = string;
while(*p != '\0'){
printf("%c\n", *p);
p += 4;
}
return 0;
}
这正确地将输出给我:
W
T
H
现在,我尝试了另一种方法,根据我对指针算法的了解,这种方法应该可行,因为我机器上的 int 大小是 4 个字节。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char const *argv[]){
char* string = "WhatTheeHell";
int* p = (int*)string;
while(*p != '\0'){
printf("%c\n", *p);
p += 1;
}
return 0;
}
当我为此打印输出时,它给了我 WTH 后跟四个换行符。
我的问题,为什么要打印四个换行符?难道不应该是在打印 H 之后,*p 在字符串文字的末尾给出一个 NULL 字符并且循环终止吗?
最佳答案
对于初学者来说,第一个程序是无效的。如果一个字符串的字符数不能被 4
整除,您将有未定义的行为,因为指针可以使用指针算法指向字符串之外
p += 4;
演示程序中显示的有效方法如下所示
#include <stdio.h>
int main( void )
{
const char *s = "WhatTheeHell";
const size_t STEP = 4;
if ( *s )
{
const char *p = s;
do
{
printf( "%c\n", *p );
for ( size_t i = 0; i < STEP && *p; ++i, ++p );
} while ( *p );
}
}
它的输出是
W
T
H
第二个你的程序也是无效的。对于初学者来说,字符串文字不必按 sizeof( int )
对齐(此外,通常 sizeof( int )
可以大于或小于 4)。所以程序已经有未定义的行为。
在这种情况下
*p != '\0'
将 sizeof( int )
字节(不是单个字节)与整数常量 '\0'
进行了比较。因此,当指针指向字符串内部时(如果没有零字节,甚至超出字符串范围),比较将始终为 true
。
顺便还要注意,在 C 中 sizeof( '\0' )
等于 sizeof( int )
。
关于c - 使用指针遍历字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57109484/