理解将 `strlen` 应用于带有 `char *` 强制转换的整数数组。

huangapple go评论60阅读模式
英文:

Understand Strlen applied to int array with char * cast

问题

I'm currently stuck on this problem.

I have the following code:

int v[10] = {-1, 1000, 2};

int a;

a = strlen((char *)v+1);

printf("strlen result for (char *) v+1: %d\n", a);

I cannot understand why the final result is always 5 on a little endian "machine".
According to strlen documentation, the char * cast in this situation should return 4.

I tried also in an online compiler and other machines, but the result remains the same. What am I missing?

英文:

I'm currently stuck on this problem.

I have thefollowing code:

    int v[10] = {-1, 1000, 2};
    
    int a;
    
    a = strlen((char *)v+1);
    
    printf("strlen result for (char *) v+1: %d\n", a);

I cannot undestand why the final result is always 5 on a little endian "machine".
According to strlen documentation, the char * cast in this situation should return 4.

I tried also in online compiler and other machines but the result remains the same. What am I missing ?

答案1

得分: 5

最可能的是 V 中的字节表示为:

0xff, 0xff, 0xff, 0xff, 0xe8, 0x03, 0x00, 0x00,  ...

所以在跳过第一个字节之后(而不是第一个整数,因为你将其转换为 char *),在第一个 '\0' 字符之前有 5 个字节。

英文:

Most likely the bytes in V are represented as:

0xff, 0xff, 0xff, 0xff, 0xe8, 0x03, 0x00, 0x00,  ...

So after skipping the first byte (not the first int, since you cast to char *) there are 5 bytes before the first '\0' character.

答案2

得分: 4

不应在整数数组上使用 strlen。函数的名称已经表明它只应用于合法形式的C字符串。

也就是说,出于学习的目的,对于小端序机器上的数组 v,其内存布局看起来像这样:

    ff ff ff ff e8 03 00 00 02 00 00 00 (00 向后)
低 0>-^^----<0 1>-------<1 2>-------<2 3> ...----<9 高

由于你在调用 strlen((char *)v + 1),计算从我标记为 ^ 的位置开始。有5个非零元素 (ff, ff, ff, e8, 03),所以结果是5,而不是4。请记住 1000 等于 0x3e8。

英文:

You should not use strlen on an int array. The function's name already suggested it should only be used on legally formed C strings.

That being said, for the reason of studying, array v's memory layout looks something like this on a little-endian machine:

    ff ff ff ff e8 03 00 00 02 00 00 00 (00 onwards)
low 0&gt;-^^----&lt;0 1&gt;-------&lt;1 2&gt;-------&lt;2 3&gt; ...----&lt;9 high

Since you are calling strlen((char *)v + 1), the computation starts at the location I marked with ^. There're 5 non-zero elements (ff, ff, ff, e8, 03), so the result is 5, not 4. Remember 1000 == 0x3e8.

答案3

得分: 3

这个小演示可以帮助你澄清小端机器中字节的顺序(假设一切都按上文所写):

char* z = (char*)v + 1;
for (int i = 0; i &lt; 10; i++)
	printf(&quot;%02x &quot;, (uint8_t)z[i]);

输出应该像这样:
ff ff ff e8 03 00 00 02 00 00(就像Harlan Wei上面展示的一样)。

这个小片段还可以帮助你看到在大端机器中字节是如何存储的。因此,简单的计数应该让你得到5,而不是4,就像strlen(...)试图说明的那样。

英文:

This little demo can help you clarify how bytes are ordered in little-endian machine (presuming every thing is as written above):

char* z = (char*)v + 1;
for (int i = 0; i &lt; 10; i++)
	printf(&quot;%02x &quot;, (uint8_t)z[i]);

the print out should be like this:
ff ff ff e8 03 00 00 02 00 00 (like Harlan Wei showed above).

this little snippet can also help you see how bytes are stored in big-endian machines too. So mere counting should give you 5 not 4 as strlen(...) was trying to say.

huangapple
  • 本文由 发表于 2023年2月19日 03:07:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495741.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定