1

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?

SZYoo
  • 147
  • 7
  • 6
    Pointer difference counts number of elements, not bytes. – interjay Jul 04 '23 at 13:16
  • 1
    The `sizeof` operator results in a value of type `size_t`, the `printf()` conversation specifier for `size_t` needs a `z` length modifier. Means use `%zu` to print `sizeof szStr` not `%u`. Otherwise you can create UB. For pointers, cast them to `(void*)` (most platforms will work without this, but it is technically UB to not cast them to `(void*)` (assuming `TCHAR` isn't a `char`). – 12431234123412341234123 Jul 04 '23 at 13:34
  • 1
    The comment of @interjay is really the answer. It has been explained in several questions, e.g.: https://stackoverflow.com/questions/11713929/c-c-pointer-arithmetic – nielsen Jul 04 '23 at 13:45
  • @12431234123412341234123 just put this piece of code in a main function, then it works. Header files required are , . – SZYoo Jul 04 '23 at 14:09
  • @SZYoo No it doesn't (`error: unknown type name ‘TCHAR’ `). And there is no `` in the C standard nor on my platform. Please specify the platform and libraries you use in the question. Is it so hard to create a working example for the question? – 12431234123412341234123 Jul 04 '23 at 14:37
  • @12431234123412341234123 the platform is win10. You can create a win32 console project in Visual Studio and copy-paste the code in the main function. Of course dont forget include header files forementioned. Sorry for vague illustration. – SZYoo Jul 04 '23 at 14:51
  • @SZYoo And why didn't you add this information to the question? A Windows tag or something like that would already help a lot. No i can't, Visual Studio isn't supported on my platform. – 12431234123412341234123 Jul 04 '23 at 14:55
  • @12431234123412341234123 it's my fault. I add it now. – SZYoo Jul 04 '23 at 14:56

1 Answers1

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.