Making Peace with Double Pointers

  Рет қаралды 68,034

Jacob Sorber

Jacob Sorber

Күн бұрын

Пікірлер: 104
@Marco-yj6gg
@Marco-yj6gg 2 жыл бұрын
3:39 `char *str;` is actually a pointer to a single character.
@mixed7991
@mixed7991 4 жыл бұрын
2:50 bruh........... That short explanation just saved me a lot of brain cells. Thx so much.
@nabiisakhanov3522
@nabiisakhanov3522 4 жыл бұрын
Man it's so cool! Learning more about C makes me understand higher-level languages better. For example, nesting objects into arrays into objects etc. seemed like a magic in JS, but now it's clearly just stacking pointers under the hood! 🤯
@mathew5880
@mathew5880 2 жыл бұрын
Where were you all my life dude you cleared a major confusion i have been having in pointers.you deserve more attention for the quality of your content .thanks alot for this video
@sschulze2891
@sschulze2891 2 жыл бұрын
typedef can be your friend when dealing with triple or quadruple pointers. I just had to make a triple pointer in a project wherei had an array of pointers and i wanted to have a function that populates this array - naturally ending up with populate(type*** list) as an argument in my function. While this compiles and works without issue it is still hard to read so i used typedef type** typeList which reduced the argument to a single pointer again: populate(typeList* list) I think this very handy in keeping the intention of the code clear instead of asking why there are three goddamn astrisks.
@msk0693
@msk0693 3 жыл бұрын
Another use case I see is when you want to know what's in front of you but also where you came from. For example using a double pointer to traverse a linked list. Say we want to add an element in alphabetical order to our list. When can attach our double pointer to any of the links. This allows us to peak at the next element we are pointing at to check if the item we are inserting is going in the right place. If the next element is further in the alphabet all we need to do is deference our double pointer and attach the new element at our current memory address as our double pointer was in the correct address for the insertion. If I'm not mistaken this is how lists are handled in linux!
@neroabey9220
@neroabey9220 2 жыл бұрын
1:04 example **p2=5 leads to segmentation fault, correction define a new variable int x=5 then assign x address pointer to *p2=&x
@xCwieCHRISx
@xCwieCHRISx 2 жыл бұрын
I did get double pointers when i learned references in c++. double pointers are used as a reference of a pointer in c. Or iterating through matrix. But when people use arrays or matrix they really should use the [] operator instead of * to do dereferencing in my opinion. The programmers mind is more trained with this notation because of other languages.
@Psykorr
@Psykorr 3 жыл бұрын
Another thing about what the type to a pointer does besides telling the compiler what type to return when dereferencing is it says how many bytes to increment at a time. For example a char* ptr, would increment by 1 byte from the Base address of ptr if you increment it but the int* ptr would jump 4 bytes for each increment. It jumps sizeof(what ever its type) bytes from its current location in address.
@realdragon
@realdragon Жыл бұрын
I tried to look through internet but I couldn't find any easy example of double pointer used in 2D array. They just say "You can use it there" but don't show how
@thisisreallyme3130
@thisisreallyme3130 2 жыл бұрын
Fantastic lecture, Jacob, thanks. A small bit of feedback -- during times like 00:36, could you vocalize more what "this... and this" are (pretend the audience were somewhat vision impaired). My favorite YT programming talks are the kind I can just "listen" to (podcast style) in the shower or when driving. (I'd also imagine the extra identification of key points would lend some accessibility boost to others as well)
@JacobSorber
@JacobSorber 2 жыл бұрын
Thanks. I'll see what I can do in future videos.
@scorpionesp6510
@scorpionesp6510 2 жыл бұрын
what syntax highlighting are you using? looks so crispy
@charleshwankong
@charleshwankong 3 жыл бұрын
I read somewhere that instead of trying to catch exceptions in C, it's more natural to initialize and set flags manually as needed. This idea was then extended to the usage of pthreads: storing flags as a thread-level variable that the writer maintained him/herself. I'm wondering which is more often used for such a responsibility in the code fragment in the video: pthread_join()'s return value or its double pointer parameter?
@tonychan6930
@tonychan6930 3 жыл бұрын
0:45 Shouldn't the address of the integer pointer be 0x006AC instead of 0x006AD? Since char is one byte, therefore 0x006AB + 1 is equal to 0x006AC. Please correct me if I am wrong. BTW, thanks for all your hard work. You don't realize how much your channel has helped me. God bless!
@Hauketal
@Hauketal 2 жыл бұрын
Often the compiler generates padding for alignment, because accesses are faster that way. But yes, especially 0x6AC should still be better as it is even.
@peacemekka
@peacemekka 3 жыл бұрын
I'm totally losing it even looking at a single pointer. I can't really wrap my mind around the concept of a pointer.
@jackdeansmith
@jackdeansmith 7 жыл бұрын
Thanks for explaining the pthread_join example, that was very helpful!
@JacobSorber
@JacobSorber 7 жыл бұрын
You're welcome. Glad it helped.
@rudragaur6248
@rudragaur6248 3 жыл бұрын
Was that sarcasm?
@OMNI_INFINITY
@OMNI_INFINITY 6 ай бұрын
*Comment 100! Yay! So it honestly looks like C already had a way to pass by ref and so there had to be a distinct way to pass a pointer by ref, so thus ** when passing a pointer by ref. Seems logical?*
@brucelytle1144
@brucelytle1144 Жыл бұрын
I haven't watched this yet, I wanna see your solution. I once had a need for a 5th level pointer (long story!) I asked a friend that taught C @ UCSC. It stumped her and I have forgotten my solution! That was 30+ years ago! The use of and ability to manipulate pointers is one of the most useful aspect of C to me. Thanks for the pointers (no pun intended!) and tips on C. I'm picking it back up, after 25 years of no programming. Ok, watched it. I think the problem was the compiler I was using, Microsoft C. It would choke at 5 levels of indirection, handled 4 levels just fine. Compilers have come along way in 30 years! Can't remember needing that deep indirection with gcc.
@davidciocoiu4900
@davidciocoiu4900 4 жыл бұрын
So if my understanding is correct, passing just the pointer to mythread_join would be like passing by value, meaning we are just creating a copy of the pointer. Whereas using a double pointer would allow us to modify the original retval parameter. Is that correct? :)
@JacobSorber
@JacobSorber 4 жыл бұрын
Right. C passes arguments by value. So, when you pass a pointer - a pointer is basically a reference, so it's like passing by reference (at least in what it allows you to do to the argument).
@xtremeblaze777
@xtremeblaze777 3 жыл бұрын
To generalize the first application, anytime you want to modify the pointer rather than pointee, use double pointers. It’s the same idea behind passing a reference to a regular variable bc you want to modify that variable. I recall having to use reference to a pointer in a LinkedList implementation. I was using C++ so I used &* but I think ** would work as well.
@kitamashi
@kitamashi Жыл бұрын
yes I ran a few tests and it seems that passing &var works fine if curious: #include #include #include void display(int *output) { printf("%i ", *output); printf("%i, %i ", output[0], output[1]); output[0] = 4; printf("%i ", *output); printf("%i, %i ", output[0], output[1]); } int main(int argc, char *argv[]) { int number[2] = {0,0}; number[0] = atoi(argv[1]); number[1] = atoi(argv[1])+1; printf("number: %i address: %i points to: %i ", number, &number, *number); display((int*)&number); printf("%i, %i ", number[0], number[1]); printf("number: %i address: %i points to: %i ", number, &number, *number); // free(number); return 0; }
@tattikhatti7917
@tattikhatti7917 2 жыл бұрын
used to make dynamic 2 dimensional array
@黎銘-s9n
@黎銘-s9n 3 жыл бұрын
got it. multitude of n pointers in a row for n-1 dimensions of arrays, a typical use case of pointers in a row:)
@MatheusAugustoGames
@MatheusAugustoGames 4 жыл бұрын
I use double pointers when dealing with data structures if I want to pass to a function the connection a node has with another node, without having to give the function access to other properties of the node
@RmDIrSudoSu
@RmDIrSudoSu 4 жыл бұрын
For arrays I would tend to use single pointer, the move on the second or third dimension using multiply, using this require less jump. So I would often tell student not to use ** in that case. The other example is a great one of where it can be use.
@TheCobaqua
@TheCobaqua 2 жыл бұрын
agreed
@ian_snyder
@ian_snyder 11 ай бұрын
Clearest explanation I've found, thanks!
@mumatim
@mumatim 4 жыл бұрын
I dont get it why int **x is equal to *x[]
@JacobSorber
@JacobSorber 4 жыл бұрын
Consider checking out my video about the relationship between pointers and arrays. kzbin.info/www/bejne/q4WQin97fdyJiZY
@mumatim
@mumatim 4 жыл бұрын
@@JacobSorber Thanks for the fast response. Professor would you mind explaining to me how to improve my C skills. Every time I start a free basic course is quite easy for me. However, when I look something up on Stackflow coding there seems impossible to understand
@rustycherkas8229
@rustycherkas8229 2 жыл бұрын
Your two examples are equivalent, but should be used appropriately. The *pArr[ ] version suggests that pArr may/can/will be used to iterate-through or index into an array of contiguous pointers. The **pXXX version suggests that (*pXXX) points at a single blob of interesting data (a struct, perhaps), but the value of pXXX may be set to point to any many scattered 'blobs'. Both are "pointers to pointers", but used in different ways. By rights, the simple char *str = "ABC"; should be written as char str[] = "ABC"; but few coders are that meticulous....
@bettyswunghole3310
@bettyswunghole3310 5 ай бұрын
It's not so much the concept of double pointers that's difficult...it's understanding the right syntax to make them do what you want...
5 жыл бұрын
Don't want to start a flame war here, but, in my opinion (and it's just my opinion!), pointers are "scary" basically because of the way they are written. It is so much easier to get it all when we write int* x, instead of int * x (had to add a space between * and x because youtube formats it as bold). The latter is confusing because it seems like a dereference. The former is easier to read because it feels like int* is a "kind of" a type. But then, because of one single exception (that I think should really be avoided in the first place), when you declare things using the comma (int * x, * y, * z), often we listen that one should really use the form int * x, instead int* x. But if that's the case, what would be the explanation for using int * x when you declare a pointer and using int* func() {...} when you declare a function? Isn't it easier to write it the same way for variable declarations and function declarations? In my experience, always using int* x (and then, of course, int* x; int* y; int* z) really makes much, much more sense. Although int* is not a "type", it really makes things easier if you think they are "kind of" a type. It is really easy to realize that int* x[] is an array of pointers to int, like we have a lot of int*'s side by side, instead of int * x[], which makes everything much more confusing. I know it varies from person to person. Just wanted to share my personal experience if you want to try a different way of looking at pointers. It has helped me a lot.
@JacobSorber
@JacobSorber 5 жыл бұрын
Thanks. Yeah, I think you should definitely use whatever style "speaks to you" and helps you better keep track of what you're doing. But, I also think it's important to be able to make sense of pointer declarations in all of their forms since if you spend much time developing software, you're going to come across all different styles, written by other developers.
5 жыл бұрын
@@JacobSorber No doubt! Thanks a lot and thanks for the awesome videos you post!
@clownworld4655
@clownworld4655 3 жыл бұрын
Having to learn as much as I can about this since my professor decided to make us do double pointers pointing to pointers pointing to structs to make some crazy wannabe array without just using and array because that’s “not allowed” yea it’s dumb
@OMNI_INFINITY
@OMNI_INFINITY 6 ай бұрын
*WHY would char** point to a 2D array? Can explain the logic in that? That wasn't explained in that video.*
@smartiee
@smartiee 20 күн бұрын
It doesn’t, it actually points to a single character. If you want to point to an array of characters you would need to use the two [] brackets
@mmkvhornet7522
@mmkvhornet7522 2 жыл бұрын
this video helped me to better understand double pointers !! i hope that make a video about using them in linked lists
@JacobSorber
@JacobSorber 2 жыл бұрын
Glad I could help. Can you tell me more about the specific double pointer challenge you're having with linked lists?
@mmkvhornet7522
@mmkvhornet7522 2 жыл бұрын
@@JacobSorber i tried to use double pointers in loops to traverse my linked list to either delete a node or insert one at a given position but i faced some hiccups 🥲 and thanks in advance sir !!
@adeled8833
@adeled8833 4 жыл бұрын
This is better than any book I’ve read
@baruchben-david4196
@baruchben-david4196 4 жыл бұрын
I'm always confused by what a null pointer is/does...
@ElPikacupacabra
@ElPikacupacabra 3 жыл бұрын
A "null pointer" is a pointer holding address 0. It is very unlikely that what you want to address is exactly there, so it became also a convention for how an unused pointer should be set. Basically, you check for 0 to figure out if the pointer is in use, or useful.
@hugocardoso1488
@hugocardoso1488 4 жыл бұрын
So if a have a function that receives a **pointer and i need to send that to another function i would receive it as ***pointer on the second function? and that way i can modify the original value?
@JacobSorber
@JacobSorber 4 жыл бұрын
You could do that, but it would be easier to just do it with a **pointer, unless you want to modify the **pointer rather than what it points to.
@hugocardoso1488
@hugocardoso1488 4 жыл бұрын
@@JacobSorber yes i need to modify it, i use a linked list and i have a function that creates and manages the list, but i have other function that load a binary file and i want to take those values and add them to the list. So this is the right way to do it?
@KaleshwarVhKaleshwarVh
@KaleshwarVhKaleshwarVh 4 жыл бұрын
yes, I came to make peace with the pointers and you take an example of pthread, cool.
@JacobSorber
@JacobSorber 4 жыл бұрын
All roads eventually lead to both pointers and threads. :) Thanks for being part of the journey.
@stefanmilenkovic4715
@stefanmilenkovic4715 2 жыл бұрын
I think I do get double pointers, but I don't get why is int **array not the same as int *array[]? If arrays are pointers why aren't arrays of pointers the same as double pointers?
@Joao-oo8yj
@Joao-oo8yj 3 ай бұрын
But they are the same: type* array[n] = type** array.
@limitless1692
@limitless1692 4 жыл бұрын
// Your code gives Segmentation fault (core dumped) i don't think you event tested that bit of code int *p1; int ** p2; p2=&p1; **p2=5;
@JacobSorber
@JacobSorber 4 жыл бұрын
Right. That code wasn't meant to be a complete executable example. p1 needs to be set to something. Sorry if that was confusing.
@GangrelBrandao
@GangrelBrandao 2 жыл бұрын
Hello Mr. Sober. What do you think to talk about const char * const str; ?
@bizzy423
@bizzy423 7 жыл бұрын
I like your videos wish they were longer more examples more depth because you explain these hard concepts well. Would like to see video's on Classes inheritance virtual functions in c
@JacobSorber
@JacobSorber 7 жыл бұрын
Thanks, Bruce. I'm glad this is helpful. I try to add depth where I can, but time is super tight. This is a weekend/evening/when-I-get-the-time hobby for me. I'll do what I can. As for topics, I will definitely add some OOP videos in the future. My kids are learning java. So, it's on my mind a lot. I am curious about what you mean by "virtual functions in C". C doesn't have true virtual functions (C++, Java, Ruby do), but you can try to fake it a bit. Can you clarify what you're looking for there?
@bizzy423
@bizzy423 7 жыл бұрын
I deal a lot with Atmel studio and ASF and they seem to be trying to cram oop into embedded c. In a lot of their demo software I think I see them trying to create pseudo classes with structures and pointers and virtual tables containing function pointers corresponding to virtual functions. Its interesting and hair pulling at the same time as I guess this is not very common.
@JacobSorber
@JacobSorber 7 жыл бұрын
Ah, I see. Yeah, there are ways to fake it. I'll see what I can do in a future post.
@SerBallister
@SerBallister 2 жыл бұрын
@@bizzy423 That's typical for driver interfaces, Linux does the same thing. Interfaces are not the exclusive realm of C++.
@michalski9141
@michalski9141 2 жыл бұрын
very simple, great explanation
@lingaraoyechuri7880
@lingaraoyechuri7880 4 жыл бұрын
is there any way to modify the char**
@JacobSorber
@JacobSorber 4 жыл бұрын
I'm not sure I fully understand the question, but yes, you can modify double pointers just like you would a single pointer.
@lingaraoyechuri7880
@lingaraoyechuri7880 4 жыл бұрын
i am new to C++, i want to add "/n" in the end of my char**, is there any way...
@JacobSorber
@JacobSorber 4 жыл бұрын
Yes, there is. Actually, I don't have enough info to really know what you're trying to do, but it's definitely possible to add new line characters to strings in C++.
@RacoonEvil
@RacoonEvil 4 жыл бұрын
Im actually pretty sure you can’t add a newline character at the end of an array or pointers
@RacoonEvil
@RacoonEvil 4 жыл бұрын
Its also completely not necessary
@clairevicidomini3199
@clairevicidomini3199 7 жыл бұрын
is the name of an array declared like this char arr[SIZE] an lvalue or not? is it just a const pointer to variable char internally, or is it spmething more complex? Thanks :)
@JacobSorber
@JacobSorber 7 жыл бұрын
To be clear, pointers and arrays are related and in memory, they are the same, but they aren't the same to the compiler. So, I can do this. char arr[SIZE]; char *p = arr; But, I can't do this. char arr[SIZE] = p; One difference is that- sizeof(p) == 8 //assuming a 64-bit machine sizeof(arr) == SIZE And... *(p+2) is the same as... p[2] is the same as... *(arr+2) So, they aren't equivalent, but they are often interchangeable and represented the same way in memory.
@kumarshantanu3084
@kumarshantanu3084 4 жыл бұрын
my brain would have exploded if i didn't get to watch this video. thanks a lot sir : )
@JacobSorber
@JacobSorber 4 жыл бұрын
Welcome. Glad I could help.
@raydonmuregi132
@raydonmuregi132 Жыл бұрын
Thank you!
@moeenkamali1288
@moeenkamali1288 Жыл бұрын
nice tutorial thanks!
@SteelHorseRider74
@SteelHorseRider74 3 жыл бұрын
JS: "so you see, there's nothing to be afraid of..." me, who's programs segfault by default: "uhm... yeah..." o_O >_
@KangJangkrik
@KangJangkrik 3 жыл бұрын
Just imagine JS can segfault, web will not be secure as today
@Victor_Marius
@Victor_Marius 2 жыл бұрын
@@KangJangkrik I think he meant Jacob Sorber not JavaScript
@KangJangkrik
@KangJangkrik 2 жыл бұрын
@@Victor_Marius i know
@Hunter725R
@Hunter725R 4 жыл бұрын
can't wait for c25 to introduce triple pointers.
@marbens
@marbens Жыл бұрын
Triple pointers (and all other levels) were already introduced in C89
@abdelazizsaad7676
@abdelazizsaad7676 3 жыл бұрын
This is very nice explanation, thanks.
@JacobSorber
@JacobSorber 3 жыл бұрын
You're welcome. Glad you liked it.
@notgiven3971
@notgiven3971 4 жыл бұрын
thank you very much for the help
@leereyno
@leereyno 7 жыл бұрын
There is an O'Reilly book called Understanding and Using C Pointers that explains this and other pointer topics in great detail: my.safaribooksonline.com/book/programming/c/9781449344535
@JacobSorber
@JacobSorber 7 жыл бұрын
Thanks, Lee. Great resource.
@nathanlebrun9938
@nathanlebrun9938 Ай бұрын
Thanks 🙏
@ميسمنال-س1خ
@ميسمنال-س1خ 3 жыл бұрын
good job keep doing videos like this thanks
@richiekho1159
@richiekho1159 3 жыл бұрын
Hello pentapointerers
@xit
@xit 2 жыл бұрын
Always the best
@negvorsa
@negvorsa 4 жыл бұрын
char *str without context is just pointer to char, maybe nothing maybe 1or more characters and definitely it is not array of characters!?
@thedoublehelix5661
@thedoublehelix5661 4 жыл бұрын
it sorta is... basically arrays are just pointers to the start of the array and accessing the next element in the array is just moving along in memory from the start
@diego1602
@diego1602 2 жыл бұрын
You speak to fast and give things for granted!
@JacobSorber
@JacobSorber 2 жыл бұрын
Let me know what you think I missed, and I'll try to catch it in a future video.
@ceren6778
@ceren6778 2 жыл бұрын
cok iyi
@71GA
@71GA 4 жыл бұрын
You talk too fast to absorb this in one try...
@JacobSorber
@JacobSorber 4 жыл бұрын
Sorry about that. The good news is that KZbin lets you replay videos and play back at slower than realtime speeds. Hopefully one of those options helps.
Understanding the Extern Keyword in C
3:17
Jacob Sorber
Рет қаралды 75 М.
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 338 М.
When you have a very capricious child 😂😘👍
00:16
Like Asiya
Рет қаралды 18 МЛН
Support each other🤝
00:31
ISSEI / いっせい
Рет қаралды 81 МЛН
BAYGUYSTAN | 1 СЕРИЯ | bayGUYS
36:55
bayGUYS
Рет қаралды 1,9 МЛН
How to Check Your Pointers at Runtime
14:12
Jacob Sorber
Рет қаралды 32 М.
Creating Your Own Programming Language - Computerphile
21:15
Computerphile
Рет қаралды 207 М.
you will never ask about pointers again after watching this video
8:03
How to make memory read-only in your C programs.
12:57
Jacob Sorber
Рет қаралды 21 М.
The What, How, and Why of Void Pointers in C and C++?
13:12
Jacob Sorber
Рет қаралды 55 М.
why do void* pointers even exist?
8:17
Low Level
Рет қаралды 399 М.
C pointers explained👉
8:04
Bro Code
Рет қаралды 213 М.
Andrew Kelley   Practical Data Oriented Design (DoD)
46:40
ChimiChanga
Рет қаралды 151 М.
When do I use a union in C or C++, instead of a struct?
11:18
Jacob Sorber
Рет қаралды 71 М.
When you have a very capricious child 😂😘👍
00:16
Like Asiya
Рет қаралды 18 МЛН