为什么此处的Unicode地址差异不正确?

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

Why address difference between Unicode is incorrect here?

问题

TCHAR在此被解释为Utf-16,因为szStr中的每个字母占据两个字节。然而,地址差异似乎不是这样的:虽然在汉字'好'和数组开头之间有6个字母,但差异是6而不是12。有人可以向我解释原因吗?

英文:

Look at the code below,

#include<Windows.h>
#include<tchar.h>
int main()
{
	TCHAR szStr[] = TEXT("C++中文你好");
	printf("sizeof(szStr) = %u\n", sizeof(szStr)); //16
	LPTSTR lp = _tcschr(szStr, TEXT('好'));
	_tprintf(TEXT("szStr = %p, lp = %p \n"), szStr, lp); //szStr = 0000001F9F51FAA8, lp = 0000001F9F51FAB4
	_tprintf(TEXT("difference= %u"), lp - szStr); //4
}

TCHAR is interpreted as Utf-16 here because each letter in szStr occupies two bytes.However, the address difference seems not so: although there are 6 letters between then Chinese letter '好' and the begin of the array, the difference is 6 not 12. Could someone explain the reason to me?

答案1

得分: 0

Subtracting 2 pointer you get the difference in elements not in bytes.

From the C11 standard, 6.5.6 Additive operators, Paragraph 9:

> When two pointers are subtracted, ... the result is the difference of the subscripts of the two array elements ...

When you want to calculate the difference in bytes, you can either multiply the result by the size of 1 element or by casting them to a character pointer (char, unsigned char or signed char) before subtracting.

_tprintf(TEXT("difference elements= %td"), lp - szStr); //4
_tprintf(TEXT("difference size    = %td"), (lp - szStr)*sizeof *lp);
_tprintf(TEXT("difference bytes   = %td"), (char*)lp - (char*)szStr); 

The last 2 should always output the same value.

英文:

Subtracting 2 pointer you get the difference in elements not in bytes.

From the C11 standard, 6.5.6 Additive operators, Paragraph 9:

> When two pointers are subtracted, ... the result is the difference of the subscripts of the two array elements ...

When you want to calculate the difference in bytes, you can either multiply the result by the size of 1 element or by casting them to a character pointer (char, unsigned char or signed char) before subtracting.

_tprintf(TEXT("difference elements= %td"), lp - szStr); //4
_tprintf(TEXT("difference size    = %td"), (lp - szStr)*sizeof *lp);
_tprintf(TEXT("difference bytes   = %td"), (char*)lp - (char*)szStr); 

The last 2 should always output the same value.

huangapple
  • 本文由 发表于 2023年7月4日 21:01:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76612928.html
匿名

发表评论

匿名网友

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

确定