+ 3

Pointer arithmetic

What happens when we substract 2 pointers in c ?

16th Jun 2020, 5:11 PM
A J I L
A J I L - avatar
14 Respostas
+ 5
You get the relative difference of the two pointers in the memory which apart from usage in really fast switching small memory applications like DSP is pretty useless. It tells you how many blocks are between the two addresses. https://code.sololearn.com/cq70EOLR2Zrk/?ref=app
16th Jun 2020, 6:00 PM
Anubhav Mattoo
Anubhav Mattoo - avatar
+ 15
It doesn't give you differences between two addresses Instead it will show you the difference between two variables as if they are stored in an array. 
16th Jun 2020, 5:51 PM
Lamya😉
Lamya😉 - avatar
+ 5
ThanksLamya😉 Anubhav Mattoo george . Now I got it!
16th Jun 2020, 6:31 PM
A J I L
A J I L - avatar
+ 5
A pointer subtraction returns the offset between two pointers if they're part of the same memory block (otherwise it's undefined). The return type of a pointer subtraction is ptrdiff_t and can be printed with the %td format specifier. size_t is some unsigned integer type - it could be unsigned int, unsigned long, or unsigned long long (there is no guarantee which one it will be, it depends on the platform). %ld stands for long decimal (long int) %lf is for long double. If your compiler supports C99 or higher use %zu to print arguments of size_t. If you're stuck on a C89 compiler then use %lu and cast the size_t argument to unsigned long.
17th Jun 2020, 6:18 AM
Gen2oo
Gen2oo - avatar
+ 3
https://code.sololearn.com/cq13SIlvQOkG/?ref=app Actually I'm confused with this program .
16th Jun 2020, 6:16 PM
A J I L
A J I L - avatar
+ 3
4 bytes in win 32@
17th Jun 2020, 12:54 PM
Nagarajan Govindswami Iyer
Nagarajan Govindswami Iyer - avatar
+ 2
for example???
16th Jun 2020, 5:48 PM
george
george - avatar
+ 2
imagine that pointer in fact is UINT number(address in memory in hex form) minimal is 0 so it is UINT, you will anawer on your own question. what will happens!!!
16th Jun 2020, 6:07 PM
george
george - avatar
+ 1
1. Pointers are always stored as long double. 2. Your code(s) are giving you a correct output. They are telling the number of blocks of the cast type between the two pointers. The first program gives you 2 memory addresses in hex. Find their difference then divide by the size of the cast type(int in this case(sololearn app uses a 4 byte int)). You will get the answer.
16th Jun 2020, 6:24 PM
Anubhav Mattoo
Anubhav Mattoo - avatar
+ 1
MĂžrphĂšĂșs why areyou confused? Everything is ok, diference in 4 elements so everything is ok. Try to use one and the same elements in array for example 1,1,1,1,1,1,1,1 you get 4 in anyway
16th Jun 2020, 6:24 PM
george
george - avatar
+ 1
Anubhav Mattoo ??? Long double?? No maaaan double is floating pointer value in c and c++, memory is unsigned integer with 4 bytes in win 32 and 8 bytes in case of win64
16th Jun 2020, 6:29 PM
george
george - avatar
+ 1
You are welcome
16th Jun 2020, 6:34 PM
george
george - avatar
0
Since your pointers are pointers of array elements and array in memory is continued memory space have delta in 4 elements.
16th Jun 2020, 6:26 PM
george
george - avatar
0
george In C a datatype specifies the size of the container of the value. It does not define the value. You can store anything with any type in C as long as there is a correct format specifier or cast conversation for it. For reference, doule or double precision floating point value is a 4 byte entity for C and a long extension doubles that so long double is an 8 byte entity, perfect for storing addresses. C has a format specifier %p specifically for this cast type.
17th Jun 2020, 4:48 AM
Anubhav Mattoo
Anubhav Mattoo - avatar