Problems with pointer arithmetic (C)

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

Jacob Sorber

Jacob Sorber

Күн бұрын

Пікірлер: 15
@floatingblaze8405
@floatingblaze8405 3 жыл бұрын
I think you just saved me from many hours of unnecesarry frustration and banging my head into the keyboard while having 48 stackoverflow tabs open in the future, thanks!
@JacobSorber
@JacobSorber 3 жыл бұрын
Wonderful. That's the goal.
@rajeshthipse
@rajeshthipse 2 жыл бұрын
4:58 "Why they did this, I am not sure" Reason - int i[10]; char c[10]; i[2] is *(i+2) and c[2] is *(c+2)... we want 3rd element of int array... and 3rd element of char array! Though both are indexed at 2 but they're at different offsets from their starting pointers. If [2] always meant moving 2 bytes, then we will be compelled to make some calculations while getting i[2].
@menachemadin8471
@menachemadin8471 2 жыл бұрын
I just went to the comments section to write the same answer and saw that you've already did it 😅
@softwite
@softwite 4 жыл бұрын
Hi, I'm a computer sciences student and I'm asked to implement a generic qsort function : sort(void *base, size_t nel, size_t width, int (*compar)(const void *, const void *)) but I don't know how to compare the elements without type casting the elements. How am I supposed to compare elements in a void pointer without casting it?
@JacobSorber
@JacobSorber 4 жыл бұрын
By definition, a void * has no type information (except that it's a pointer). So, if you want to reason at all about what that pointer points to, you need a cast or you need to assign it to a typed pointer. You need to somehow tell the compiler how you want it to interpret that memory.
@casperes0912
@casperes0912 3 жыл бұрын
If you're to implement that function signature, you don't want to compare elements at all. That's what your *compar function pointer is for. look at how functions like qsort in C already work. Just call the compar function pointer with your arguments, you get back an integer that tells you which should be first. It's flexible that way. Though I know my reply here is probably way too late :P
@bryanterrill7674
@bryanterrill7674 3 жыл бұрын
I am curious of why your print out shows what appears to be memory offset by only 10 not 16? 0x1fb2010 and 0x1fb2020. At first I thought you might have made a mistake because essentially at a quick glance I saw the 2 and then the 8 bytes from the sizeof which then you factor in casting to a char pointer which char has a size of one would be one times two plus eight equals 10 BUT that's not actually what's going on because of simply order of operations and theres * not + (my eyes are so bad...) so my "at a quick glance" is synonymous with "I'm a doofus who didn't pay as much attention at that glance" :p so I'm still confused... Shouldn't it be that P2 should equal 0x1fb2026? Maybe I'm just missing something simple it's been one of those days. I am definitely no expert in C or pointer arithmetic -- I use it for traversing strings a lot though -- I just want to understand why I don't understand that memory readout. Anyways enjoy your videos quite a bit. Thanks for making them.
@JacobSorber
@JacobSorber 3 жыл бұрын
The addresses are in hex (base 16). So, 0x1fb2010 + 16(decimal) is 0x1fb2020
@bryanterrill7674
@bryanterrill7674 3 жыл бұрын
@@JacobSorber I'm all about honesty and I've never been much into hex as I've mostly come from scripting Lang's like js php and stuff. Anything you can do in hex there outside of protocol stuff you did without it (lol) but I've been messing with it the last week in C and trying to understand it. It's simple in concept but not so much intuitive when you're just learning it. For example I had made something that would take input a string for example and spit out the hexa for it. But I ran into a problem of never getting past zero or one, eg 7f. Now I believe -- and again please correct me if I'm wrong, because like you discuss in your #how you feel about people that cheat in your courses" video I don't want to grab other code and/or not know what I'm doing but is it 2020 because it must go 2010-2019[a-f] which is 15 digits (we had the 0 from the original 2010) and the 16th makes it (after going through 0-9,a-f) roll over so to speak to 2 ? This is what I didn't originally understand with how hex editors and hex dump code was spitting out like 3e. I had basically made a function that would take an integer number and run a switch case where if Case 10 through 15 would return capital a through f with default returning a %02d sprintf. And of course me being again kind of kind of a doofus obviously this would never return anything like 2A or really you know anything beyond 0 through 9 or a through f so I feel like this could have been my fundamental issue with the hex. I very much appreciate your prompt reply. My apologies for going to such the side story there but I feel it's kind of relevant into my misunderstanding of hex and of course the root of this problem I think if my above assumptions are correct that the 2020 actually makes sense now because while I was thinking you'd need 2026 if you actually remove the six that a through f makeup you would be at 2020.obviously you know that already Master but perhaps you being a teacher and all if I'm correct you'll get that little feeling I'm sure you get when it clicks for somebody that you're having an influence on teaching something too again appreciate your videos like most people I wish there was more and they went more in depth but again as you've mentioned this is a weekend / when you get time hobby but I see why you're a teacher you're very good at breaking down I wouldn't say complicated (in hindsight) , but often unintuitive things. Actually just since this is already quite a novel I would like to thank you for helping me understand how to work with I haven't done bit masks yet but bit fields I ran into quite the problem trying to fiddle with more than four options however because almost every tutorial has 0001,0010,0100, and 1000 but almost none go beyond that to use the first four bits of the byteso you let me down quite a good rabbit hole for the day last week on that video and whilst I haven't really utilized it outside of fiddling with it and seeing the potential I definitely plan incorporate it and now like the config for like PHP and Apache and all that makes so much more sense now as well as some of the flags you can use and see if you opt to use f open and all that with the bit fields okay I went on enough thank you bro!
@edwardmacnab354
@edwardmacnab354 2 жыл бұрын
you need to learn hex and binary guy. 0001 0000 +0001 0000 =0010 0000 which in hex is 10+10=20 where hex10 is 16 decimal and hex 20 is 32 decimal
@kamaltg4706
@kamaltg4706 3 жыл бұрын
Thank u jacob I want to know how compiler do pointer arithmetic Maybe you may know it now after 4years
@JacobSorber
@JacobSorber 3 жыл бұрын
I'm not 100% sure I understand the question, but the compiler just takes the pointer's address and generates assembly code that will increment that address by the correct offset (that is, the number you provide in your program multiplied by the size of the type it points to).
@sureshnair7732
@sureshnair7732 4 жыл бұрын
Great video
@JacobSorber
@JacobSorber 4 жыл бұрын
Thanks!
Better Portability with Standard Integers
1:13
Jacob Sorber
Рет қаралды 8 М.
Header Issues: Guards, Name Mangling, and extern "C"
8:32
Jacob Sorber
Рет қаралды 78 М.
SISTER EXPOSED MY MAGIC @Whoispelagheya
00:45
MasomkaMagic
Рет қаралды 16 МЛН
Cool Parenting Gadget Against Mosquitos! 🦟👶 #gen
00:21
TheSoul Music Family
Рет қаралды 33 МЛН
How to Check Your Pointers at Runtime
14:12
Jacob Sorber
Рет қаралды 31 М.
The What, How, and Why of Void Pointers in C and C++?
13:12
Jacob Sorber
Рет қаралды 54 М.
why do void* pointers even exist?
8:17
Low Level
Рет қаралды 378 М.
How do I access a single bit?
11:07
Jacob Sorber
Рет қаралды 21 М.
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 318 М.
Just enough C to have fun
39:29
Kay Lack
Рет қаралды 56 М.
Another way to check pointers at runtime in C
12:16
Jacob Sorber
Рет қаралды 12 М.