Can you add or subtract two pointers?

  Рет қаралды 7,083

CodeVault

CodeVault

Күн бұрын

Пікірлер: 16
@kairu9373
@kairu9373 4 жыл бұрын
You deserve more subs!
@martingeorgiev999
@martingeorgiev999 Жыл бұрын
8:47 the reason why -4 was printed is because anything allocated with malloc is aligned to a boundary that is suitable for all fundamental objects. Also, when you subtract pointers you get a value of type ptrdiff_t which is guaranteed to always be able to hold any pointer difference.
@joseluisdominguezplaza6839
@joseluisdominguezplaza6839 Жыл бұрын
Yeah I tried with an array of long long and had a different result (-1) that I think is the correct result
@mavrix2356
@mavrix2356 4 жыл бұрын
Amazing explanation 👌
@novaz4768
@novaz4768 Жыл бұрын
it wouldve been helpful to give us a work around. I was trying to add refrenced variables in a function
@CodeVault
@CodeVault Жыл бұрын
For what do you need a workaround exactly? Can you explain more in detail so I understand what you need?
@devDarshilJadav
@devDarshilJadav 4 жыл бұрын
thank you so much
@krtirtho
@krtirtho 9 ай бұрын
I wish u were my professor
@ayonsinha2075
@ayonsinha2075 3 жыл бұрын
Sir u r are amazing ....💥🔥...where are from sir ??
@CodeVault
@CodeVault 3 жыл бұрын
Thank you! I'm from Romania
@m.a2894
@m.a2894 2 жыл бұрын
Hello, can you tell me why does my program returns '1' when I write it like this: int main() { long long int a = 15; long long int b = 20; long long int c = 30; long long res = &a - &c; printf(" %d\t%p", res, &res); } but returns '2' when i write it like this: int main() { long long int a = 15; long long int b = 20; long long int c = 30; printf("%p
@CodeVault
@CodeVault 2 жыл бұрын
Huh, I am stumped as well. The only way to find out is to start looking at the differences in the assembly file. I'm sure there's something that happens there
@nero2k619
@nero2k619 2 жыл бұрын
I guess its because &b has to pushed onto stack but when you try to comment out printf("%p
@martingeorgiev999
@martingeorgiev999 Жыл бұрын
In the first case 'b' does not exist. The compiler is free to remove any junk from the code as long as it does not change the observable behavior.
@mukteshgautam4050
@mukteshgautam4050 6 жыл бұрын
It adds 8bits not 8 bytes!
@CodeVault
@CodeVault 6 жыл бұрын
The data type is a long long which is stored on 8 bytes. Adding any number to a pointer to long long would add that value times 8 bytes to the pointer. Remember, 1 byte = 8 bits. Not the other way around
What are void pointers in C?
10:05
CodeVault
Рет қаралды 31 М.
How to properly deal with dynamically allocated memory
13:44
CodeVault
Рет қаралды 9 М.
Try this prank with your friends 😂 @karina-kola
00:18
Andrey Grechka
Рет қаралды 9 МЛН
Writing Code That Runs FAST on a GPU
15:32
Low Level
Рет қаралды 580 М.
Pointer Arithmetic (Addition)
7:10
Neso Academy
Рет қаралды 254 М.
Learn Any Programming Language In 3 Hours!
22:37
Code With Huw
Рет қаралды 644 М.
POINTERS in C++
16:59
The Cherno
Рет қаралды 1 МЛН
When to free memory in C
13:45
CodeVault
Рет қаралды 10 М.
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 345 М.
Why are function pointers useful?
6:43
CodeVault
Рет қаралды 33 М.
Pointer Arithmetic (Subtraction)
7:59
Neso Academy
Рет қаралды 165 М.
Try this prank with your friends 😂 @karina-kola
00:18
Andrey Grechka
Рет қаралды 9 МЛН