PTN
PTN

Reputation: 1692

Char pointer arithmetic C

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

char *str1, *str2;

/* stuff in between */

int f = str2 - str1;

What is str2 - str1 returning? Let's say str1 = "foo" and str2 = "foobar".

Upvotes: 4

Views: 7084

Answers (3)

Haris
Haris

Reputation: 12270

Edited

The answer to this question depends on the situation.

1) In your situation, since the pointers aren't pointing at anything, the result will be garbage

2) If the two pointers are pointing at two valid memory locations in the program space, then the result will be the number bytes of memory in between those 2 location. Although that doesn't make any sense, and it is UB according to the C Spec, if the two pointers are not pointing to the same array or string. If the two pointers point at the same array or string then the result would be the number of elements in between two elements in the same array (When the pointers are pointing at those two elements inside that array). Check this SO question for examples and more explanation.

Upvotes: 3

Ishmeet
Ishmeet

Reputation: 1610

f = memory address in str2 - memory address in str1. "foo" and "foobar" are values to these memory location these pointers point to.

Example:

If str2 = 0x8000000a

and str1 = 0x80000000

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

Upvotes: 0

WhozCraig
WhozCraig

Reputation: 66194

The result as you've presented the question isn't defined. For pointer differencing to work correctly they must be from the same array sequence (dynamic, automatic, and even automatic VLA makes no difference). or the one-past address past the last element:

§6.5.6 Additive Operators (p9)

When two pointers are subtracted, both shall point to elements of the same array object, or one past the last element of the array object; the result is the difference of the subscripts of the two array elements. The size of the result is implementation-defined, and its type (a signed integer type) is ptrdiff_t defined in the <stddef.h> header. If the result is not representable in an object of that type, the behavior is undefined. In other words, if the expressions P and Q point to, respectively, the i-th and j-th elements of an array object, the expression (P)-(Q) has the value i−j provided the value fits in an object of type ptrdiff_t. Moreover, if the expression P points either to an element of an array object or one past the last element of an array object, and the expression Q points to the last element of the same array object, the expression ((Q)+1)-(P) has the same value as ((Q)-(P))+1 and as -((P)-((Q)+1)), and has the value zero if the expression P points one past the last element of the array object, even though the expression (Q)+1 does not point to an element of the array object.106

That said, if you rephrased so as to be compliant with 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 cited portion of the standard.

Upvotes: 14

Related Questions