Char C pointer arithmetic

I am learning pointer arithmetic and I came across something like this:

char *str1, *str2;

/* stuff in between */

int f = str2 - str1;

      

What's coming back str2 - str1

? Let them say str1 = "foo"

and str2 = "foobar"

.

+3


source to share


3 answers


The result, as you presented the question, is undefined. For the pointer to work correctly, they must be from the same sequence of arrays (dynamic, automatic, and even automatic VLA doesn't matter). or one past address after the last element:

Β§6.5.6 Additive operators (p9)

When two pointers are subtracted, both must point to elements of the same array object, or one after the last element of the array object ; the result is the difference between the indices of the two array elements. The size of the result is implementation-defined, and its type (signed integer type) is ptrdiff_t

as defined in the header <stddef.h>

. If the result is not represented in an object of this type, the behavior is undefined. In other words, if the expressions P

and Q

point to the elements i-th

and the j-th

array object, respectively , the expression (P)-(Q)

has the value ij if the value fits into an object of type ptrdiff_t.

In addition, if the expression P

points either to an element of the array object or past the last element of the array object, and the expressionQ

points to the last element of the same array object, the expression ((Q)+1)-(P)

has the same meaning as ((Q)-(P))+1

, and how -((P)-((Q)+1))

, and has a value of 0 if the expression P

points one after the last element of the array object, although the expression (Q)+1

does not point to an element of the array object. 106

However, if you've paraphrased to fit the rules above, something like:

#include <stdio.h>
int main()
{
    const char *p = "Hello World";
    const char *q = p+7;
    printf("%td\n", q-p);
    return 0;
}

      



Output

7

      

and the reason is explained from the given part of the standard.

+12


source


Edited

The answer to this question depends on the situation.



1) In your situation, since pointers do not point to anything, the result will be garbage

2) If the two pointers point to two valid memory locations in program space, then the result is the number of bytes of memory between the two locations. Although it doesn't make any sense, and it's UB according to the C Spec, unless the two pointers point to the same array or string. If two pointers point to the same array or string, the result is the number of elements between the two elements in the same array (when the pointers point to these two elements within that array). Check out this SO question for examples and explanations.

+2


source


f

= memory address in str2

- memory address in str1

. "foo"

and "foobar"

are the values ​​in that memory location that these pointers point to.

Example:

If a str2 = 0x8000000a

and str1 = 0x80000000

str2 - str1 = 0x8000000a - 0x80000000 
str2 - str1 = 0x0a 

      

0


source







All Articles