Thanks for the replies, everyone was useful in helping me understand how this works.
A friend sent me this piece of C code asking how it worked (he doesn't know either). I don't usually work with C, but this piqued my interest. I spent some time trying to understand what was going on but in the end I couldn't fully figure it out. Here's the code:
void knock_knock(char *s){
while (*s++ != '\0')
printf("Bazinga\n");
}
int main() {
int data[5] = { -1, -3, 256, -4, 0 };
knock_knock((char *) data);
return 0;
}
Initially I thought it was just a fancy way to print the data in the array (*yeah, I know :*), but then I was surprised when I saw it didn't print 'Bazinga' 5 times, but 8. I searched stuff up and figured out it was working with pointers (total amateur when it comes to c), but I still couldn't figure out why 8. I searched a bit more and found out that usually pointers have 8 bytes of length in C, and I verified that by printing sizeof(s) before the loop, and sure enough it was 8. I thought this was it, it was just iterating over the length of the pointer, so it would make sense that it printed Bazinga 8 times. It also was clea to me now why they'd use Bazinga as the string to print - the data in the array was meant to be just a distraction. So I tried adding more data to the array, and sure enough it kept printing 8 times. Then I changed the first number of the array, -1, to check whether the data truly was meaningless or not, and this is where I was confused. It didn't print 8 times anymore, but just once. Surely the data in the array wasn't just a decoy, but for the life of me I couldn't figure out what was going on.
3条答案
按热度按时间b1uwtaje1#
使用以下代码
可以看到
data
数组的HEX值为函数
knock_knock
打印Bazinga
,直到指向的值为0x00
,因为但是这里的指针指向的是字符,所以每个循环都指向一个字节,因此,第一个
0x00
访问数组第三个值的“第一个”字节。xsuvu9jc2#
您需要查看整数数组
data
中数据的字节表示形式。假设整数为4字节,以下表示形式给出了十六进制的数字数组
data
是以Little- Endian格式存储的这些数字。即,LSbyte首先出现。因此,函数
knock_knock
逐字节遍历这些数据,并为每个非零值打印Bazinga
。它在找到第一个零时停止,这将在8个字节之后。(Note:整数的大小可以是2或8个字节,但是考虑到你的指针大小是8个字节,我猜整数的大小是4个字节)。
fcg9iug33#
如果将十六进制的数组输出为字符数组,这里发生的情况很容易理解。
程序输出为
因此,字符串
"Bazinga"
将被输出的次数与数组中整数表示中的非零字节一样多。正如所看到的,前两个负数在其表示中没有零字节。然而,在任何情况下,数字
256
在其内部表示的最开始都有这样一个字节。因此,只要sizeof( int )
等于4
,该字符串将被输出8次。