+ 3
Pointer arithmetic
What happens when we substract 2 pointers in c ?
14 Antworten
+ 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
+ 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.
+ 5
+ 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.
+ 3
https://code.sololearn.com/cq13SIlvQOkG/?ref=app
Actually I'm confused with this program .
+ 3
4 bytes in win 32@
+ 2
for example???
+ 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!!!
+ 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.
+ 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
+ 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
+ 1
You are welcome
0
Since your pointers are pointers of array elements and array in memory is continued memory space have delta in 4 elements.
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.