ptrdiff_t (pointer arithmetic)

A

andy_dufresne

hi,
i'd a question abt taking the difference between two pointers.
for eg: if you have 2 char pointers pointing to members of an array,
you advance one till you encounter a space then take the difference
between
the two, will give the correct length of the string irrespective of
whether char is represented by 2 bytes (like in unicode)?? i believe it
would give the correct length, because the compiler is responsible for
scaling the difference when one advances a pointer to point to the next
element like ptr++, or is it that pointer difference (subtraction
between two
pointers to members of the same array) is not pointer arithmetic and we
need
to scale it??

eg problem:
Orig string - char *s;
Ptrs, char *start = s, *end = s;
int length;
while(!isspace(*end))
end++;
length = end - start;

is this mentioned somewhere in the c std?? if so could someone pt me in
the right direction.

Thanks.
 
S

Simon Biber

andy_dufresne said:
hi,
i'd a question abt taking the difference between two pointers.
for eg: if you have 2 char pointers pointing to members of an array,
you advance one till you encounter a space then take the difference
between
the two, will give the correct length of the string irrespective of
whether char is represented by 2 bytes (like in unicode)?? i believe it
would give the correct length, because the compiler is responsible for
scaling the difference when one advances a pointer to point to the next
element like ptr++, or is it that pointer difference (subtraction
between two
pointers to members of the same array) is not pointer arithmetic and we
need
to scale it??

eg problem:
Orig string - char *s;
Ptrs, char *start = s, *end = s;
int length;
while(!isspace(*end))
end++;
length = end - start;

This gives you the number of bytes from the start until the first
whitespace character (' ', '\f', '\n', '\r', '\t', '\v' in the C locale,
and possibly others in your locale). If there is no whitespace character
in the string, it will send your pointer off the end of the array, into
undefined behaviour land.
is this mentioned somewhere in the c std?? if so could someone pt me in
the right direction.

Since your pointers are defined as 'char', they increment one byte at a
time. The difference between them is the number of bytes. If your string
contains multibyte characters (in an encoding such as UTF-8 or native
Chinese, Japanese, Korean encodings), then the number of bytes will
differ from the number of characters. You can check the number of
characters in a string with:
mbstowcs(NULL, s, 0)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top