You're the best I found on UA-cam explaining C so far! Everytime I'm looking for something and I see you did the video on it. I feel relieved because I know I will understand it and be able to use it in just few minutes. Thank you so much
This is one of the excellent topic covered. For this particular video whoever want to get the complete understanding run the code by your own and print the pointer address which will make you crystal clear. Great Job CodeVault. Your videos are tremendous helpful.
Honestly, I've been dealing with pointers for a while now and I really haven't been able to get a grasp on this topic until now. Beautifully explained, excellent video. :)
C syntax is very confusing when it comes to pointers. In the last statement printf("After the call: %s ", str); , str is a pointer variable which holds the address of "This is a test" so I would expect to take the value by dereferencing the str variable like this.... printf("After the call: %s , *str); But it is the other way around. This is so much confusing for me, it's a nightmare.
Well, the thing is, strings don't really exist in C as a data type. They are always an array of characters. That's why you have to pass str, since, a pointer can represent an array of something (characters in this case). If we were to dereference (using *str) you would get just 1 character.
Underated channel ! Thank you so much for your work ! We would love to have a very detailed video about subtilities concerning pointers and chained lists. Because I think there is subtilities between passing a double pointer to a function and a simple pointer. Anyway thanks so much !
There's actually a full playlist regarding linked lists: ua-cam.com/video/uBZHMkpsTfg/v-deo.html There are places where double pointers are used and I explain for the most part what's up with them (if you have questions you can leave them in the comments of those videos but make sure to read some of my replies there as a lot of the questions I already answered regarding double pointers when handling linked lists)
@Sergiu, many thanks for another gem! It was one of those moments when I felt I got the point but the things became tricky in practice, and I still needed to digest the gist of the problem for some time. :o) One needs to watch the whole video, especially the part starting from 5:39, very carefully to grasp the _WHY_ aspect of using double pointers (versus the _HOW_ aspect explained earlier in the video). Anyway, after digging some more, I think I found the following keywords useful to better understand Sergiu's explanation: _variable scope_ , _pointer content, a.k.a address of the first char of the string_ (str) vs. _pointer address_ (&str) vs _pointer dereferencing_ (*str), _data sharing between functions_ and _memory management_ in C. AFAIU, the gist of this video is in C dealing with string literals (behind the scenes) as constants (not variables), hence the need to use a double pointer to redirect the same pointer to a _different memory address_ when one needs to get a new string. C makes a trick pretending to "change" the value of the pointer, but in fact points to another physical memory address for the new string. *Question:* Now what happens to the string literal when the same pointer now points to another one, e.g.: [const] char* str = "This text" ; str = "Another text"; Does it become a garbage in the memory? Thank you!
I think somehow you're misunderstanding it. There's no tricks that C does when changing a pointer. int a = 5; int* p = &a; int** q = &p; Dereferencing q once will lead you to the value of p. Dereferencing it twice will lead you to the value of a. Dereferencing p once will get you also the value of a. Regarding your question: String literals are stored in a special place in memory (not the heap nor the stack) similar to global memory. Thus, the answer is: nothing. Nothing happens to that string literal, it's still there.
@@CodeVault Thank you! By 'making a trick', I meant that using a double pointer in C to change the value of a pointer set to a *string literal* you don't actually change or modify the value of that pointer but redirect it to another location in memory, since string literals set through pointers (versus arrays) are constants, aren't they? See my snippet above.
This is where you're wrong: "you don't actually change or modify the value of that pointer but redirect it to another location in memory" Changing the value of a pointer IS THE SAME as redirecting it to another location. str in your example is a pointer. The "Another text" literal returns a const char* which is then assigned to this str. So you are changing the value of the pointer. But NOT the string it's pointing towards. On our shop, there's a PDF file that explains how pointers get assigned and I hope that helps out a bit: code-vault.net/product/ea173333939c13ed960dea60b2007938
Nice -Thanks I do have a question though. When in main you have a void function(char ***s1, char ***s2) main() { char **s1 // list of fields char **s2; // list of data for the fields. function(&s1, &s2); // this function splits the string into fields and data. } How should one handle that inside the function so that the list back in main can be printed or used elsewhwere? void function(char ***s1, char ***s2) { // get the number of rows... // allocate enough pointers based on number of rows. malloc ... // How would add values? I know strdup("") will allocate the memory for the actual string. But, after that I just get errors :) // 1 **s1 = strdup(Field1); **s2 = strdup(data): // or 2 *s1[0]= strdup(Field1); *s2[0]= strdup(data): // seems like I am so off in both cases. Back in main it seg faults. }
**s1 = strdup(Field1); I'm assuming "**s1" fails due to the pointers not being initiated. You will need to do something like this in the function "function": *s1 = malloc(sizeof(char*) * N); // N is the number of pointers you want to initialize
So basically since you can't do &&&address, ideally double pointers and further mainly serves when reuising values inside functions called inside other functions, right? Because other than that, in a practical case I could just declare a char array and refer it using a regular pointer with the display function
You're right. Other practical uses include: dynamically allocated multidimensional arrays and modifying a linked list. They are easy to get wrong so, if you can, just use a regular pointer to your data, please do so, it's going to cause less headaches.
Hmm, this is a quirk with literal strings in C. Basically when I have something like this: char* c; c = "This is a test"; You would think that, since c is not pointing to anything it should cause issues but, in fact, C automatically allocates literal strings (the ones between double quotes) inside a special place in memory (similar to global memory). And, when the literal string is used (like above), it just returns a const pointer to that place in memory.
I made a video regarding this question here: code-vault.net/lesson/he9jn5x8b1:1603733522083 Also this one is related to strings specifically: code-vault.net/lesson/0lvl47m1uh:1603733521536 Hope this helps
Summarizing: 'char mystr[] = "foo";' allocates an array in stack memory area. Same as 'char mystr[] = {'f', 'o', 'o', '\0'};'. And 'char *mystr_ptr = "bar";' allocates a char pointer "mystr_ptr" in stack memory area, pointing to a string "bar" located in read-only memory area. This is the same as 'const char *mystr_ptr = "bar";'
Sir at 00:12 you said , " these characters are stored somewhere in global memory . " Sir , can you please explain this concept to me ( in brief ) or a video on it as why these characters not being global and are still stored in data segment of our program's memory?
No, you don't need to. char* str = "This is a test"; The string "This is a test" is considered a literal and is stored in some special place in memory where all the strings go which is very similar to global memory. When the program finishes its execution it will free it automatically, no need to worry about it. You would need to free the memory if, instead, you would do something like this: char* str = malloc(sizeof(char) * 100); strcpy(str, "This is a test"); str = "This is another string"; // This would cause a memory leak
I think you're confusing between declaration and operators. When you use the asterisk near a data type (like "int* p") you're declaring a pointer. But when you're adding an asterisk before a pointer that has been declared (x = *p + 1) that means DEREFERENCING. There's a video on this topic and I know it can be confusing: code-vault.net/lesson/cw3spqag9u:1603733522844
It's more or less the same thing. I just like using char* argv[] because it signifies that it is an array of strings in C, not just a pointer to something
@@CodeVault ..that was my point, amigo! It is the most common occurrence of this and a good example. You are correct in representing this as you do _as it makes things much clearer_ to the new C programmer.
Encountering double pointers for the first time be like:
"This is getting out of hand. Now there are TWO of them."
You're the best I found on UA-cam explaining C so far!
Everytime I'm looking for something and I see you did the video on it. I feel relieved because I know I will understand it and be able to use it in just few minutes.
Thank you so much
This is one of the excellent topic covered. For this particular video whoever want to get the complete understanding run the code by your own and print the pointer address which will make you crystal clear.
Great Job CodeVault. Your videos are tremendous helpful.
Honestly, I've been dealing with pointers for a while now and I really haven't been able to get a grasp on this topic until now. Beautifully explained, excellent video. :)
Omg thx so much! Ur explanations are terrific and truly explain the intuition behind these concepts! Thanks again!
Great explanation and demonstration, thanks!
C syntax is very confusing when it comes to pointers.
In the last statement
printf("After the call: %s
", str); , str is a pointer variable which holds the address of "This is a test" so I would expect to take the value by dereferencing the str variable like this.... printf("After the call: %s
, *str); But it is the other way around. This is so much confusing for me, it's a nightmare.
Well, the thing is, strings don't really exist in C as a data type. They are always an array of characters. That's why you have to pass str, since, a pointer can represent an array of something (characters in this case). If we were to dereference (using *str) you would get just 1 character.
@@CodeVault Thank you
@@CodeVault Ok but according your diagram, str is the address of "T", but in your code str returns the string... ?
I really like how you explain things! Well done and thanks :)
Bro you are the best teacher ever. Please do consider a career in teaching. You'll be a hit.
Great Job Code Vault. Thanks for the valuable info.
The pauses that this dude made between sentences reflects how difficult is understand the pointers in c language
Underated channel ! Thank you so much for your work !
We would love to have a very detailed video about subtilities concerning pointers and chained lists. Because I think there is subtilities between passing a double pointer to a function and a simple pointer. Anyway thanks so much !
There's actually a full playlist regarding linked lists: ua-cam.com/video/uBZHMkpsTfg/v-deo.html
There are places where double pointers are used and I explain for the most part what's up with them (if you have questions you can leave them in the comments of those videos but make sure to read some of my replies there as a lot of the questions I already answered regarding double pointers when handling linked lists)
Currently in last week of Principles of Programming Languages. Thank you!
You have an immense clarity 👍
I understood by your video, double, triple, n pointers are just hierarchies used to modify lower "rank" pointers.
@Sergiu, many thanks for another gem! It was one of those moments when I felt I got the point but the things became tricky in practice, and I still needed to digest the gist of the problem for some time. :o)
One needs to watch the whole video, especially the part starting from 5:39, very carefully to grasp the _WHY_ aspect of using double pointers (versus the _HOW_ aspect explained earlier in the video). Anyway, after digging some more, I think I found the following keywords useful to better understand Sergiu's explanation: _variable scope_ , _pointer content, a.k.a address of the first char of the string_ (str) vs. _pointer address_ (&str) vs _pointer dereferencing_ (*str), _data sharing between functions_ and _memory management_ in C. AFAIU, the gist of this video is in C dealing with string literals (behind the scenes) as constants (not variables), hence the need to use a double pointer to redirect the same pointer to a _different memory address_ when one needs to get a new string. C makes a trick pretending to "change" the value of the pointer, but in fact points to another physical memory address for the new string.
*Question:* Now what happens to the string literal when the same pointer now points to another one, e.g.:
[const] char* str = "This text" ;
str = "Another text";
Does it become a garbage in the memory? Thank you!
I think somehow you're misunderstanding it. There's no tricks that C does when changing a pointer.
int a = 5;
int* p = &a;
int** q = &p;
Dereferencing q once will lead you to the value of p. Dereferencing it twice will lead you to the value of a. Dereferencing p once will get you also the value of a.
Regarding your question: String literals are stored in a special place in memory (not the heap nor the stack) similar to global memory. Thus, the answer is: nothing. Nothing happens to that string literal, it's still there.
@@CodeVault Thank you! By 'making a trick', I meant that using a double pointer in C to change the value of a pointer set to a *string literal* you don't actually change or modify the value of that pointer but redirect it to another location in memory, since string literals set through pointers (versus arrays) are constants, aren't they? See my snippet above.
This is where you're wrong:
"you don't actually change or modify the value of that pointer but redirect it to another location in memory"
Changing the value of a pointer IS THE SAME as redirecting it to another location.
str in your example is a pointer. The "Another text" literal returns a const char* which is then assigned to this str. So you are changing the value of the pointer. But NOT the string it's pointing towards. On our shop, there's a PDF file that explains how pointers get assigned and I hope that helps out a bit: code-vault.net/product/ea173333939c13ed960dea60b2007938
Your explanations are so good man!
great video. I practiced with your example. I learned alot. thanks!
Nice -Thanks
I do have a question though.
When in main you have a
void function(char ***s1, char ***s2)
main()
{
char **s1 // list of fields
char **s2; // list of data for the fields.
function(&s1, &s2); // this function splits the string into fields and data.
}
How should one handle that inside the function so that the list back in main can be printed or used elsewhwere?
void function(char ***s1, char ***s2)
{
// get the number of rows...
// allocate enough pointers based on number of rows. malloc ...
// How would add values? I know strdup("") will allocate the memory for the actual string. But, after that I just get errors :)
// 1
**s1 = strdup(Field1);
**s2 = strdup(data):
// or 2
*s1[0]= strdup(Field1);
*s2[0]= strdup(data):
// seems like I am so off in both cases. Back in main it seg faults.
}
**s1 = strdup(Field1);
I'm assuming "**s1" fails due to the pointers not being initiated.
You will need to do something like this in the function "function":
*s1 = malloc(sizeof(char*) * N); // N is the number of pointers you want to initialize
@@CodeVault Thank you. I did just that and all is perfect.
So basically since you can't do &&&address, ideally double pointers and further mainly serves when reuising values inside functions called inside other functions, right? Because other than that, in a practical case I could just declare a char array and refer it using a regular pointer with the display function
You're right. Other practical uses include: dynamically allocated multidimensional arrays and modifying a linked list. They are easy to get wrong so, if you can, just use a regular pointer to your data, please do so, it's going to cause less headaches.
Didn’t you have to malloc before assigning str a longer string “another test”?
Hmm, this is a quirk with literal strings in C. Basically when I have something like this:
char* c;
c = "This is a test";
You would think that, since c is not pointing to anything it should cause issues but, in fact, C automatically allocates literal strings (the ones between double quotes) inside a special place in memory (similar to global memory). And, when the literal string is used (like above), it just returns a const pointer to that place in memory.
Thank you sir for this series.
are those two the same?just without one step? i dont get it.
char str []="sth";
char* pstr = str;
and
char* pstr="sth";
I made a video regarding this question here: code-vault.net/lesson/he9jn5x8b1:1603733522083
Also this one is related to strings specifically: code-vault.net/lesson/0lvl47m1uh:1603733521536
Hope this helps
@@CodeVault thx❤
Summarizing: 'char mystr[] = "foo";' allocates an array in stack memory area. Same as 'char mystr[] = {'f', 'o', 'o', '\0'};'. And 'char *mystr_ptr = "bar";' allocates a char pointer "mystr_ptr" in stack memory area, pointing to a string "bar" located in read-only memory area. This is the same as 'const char *mystr_ptr = "bar";'
Hello, thank you for your great explanation.
So it's a pointer to a pointer?
Sir at 00:12 you said , " these characters are stored somewhere in global memory . "
Sir , can you please explain this concept to me ( in brief ) or a video on it as why these characters not being global and are still stored in data segment of our program's memory?
I will look into it
@@CodeVault Thank You sir for this reply😀
When you changed the str pointer from “This is a test” to “This is another test” do you need to free the memory taken by “This is a test”?
No, you don't need to.
char* str = "This is a test";
The string "This is a test" is considered a literal and is stored in some special place in memory where all the strings go which is very similar to global memory. When the program finishes its execution it will free it automatically, no need to worry about it.
You would need to free the memory if, instead, you would do something like this:
char* str = malloc(sizeof(char) * 100);
strcpy(str, "This is a test");
str = "This is another string"; // This would cause a memory leak
then why printf on display fungction have another pointer? u said double pointer already pointed to the string "this is a test"
I think you're confusing between declaration and operators. When you use the asterisk near a data type (like "int* p") you're declaring a pointer. But when you're adding an asterisk before a pointer that has been declared (x = *p + 1) that means DEREFERENCING. There's a video on this topic and I know it can be confusing: code-vault.net/lesson/cw3spqag9u:1603733522844
What Compiler are you using?
In this video it's MSVC but I usually use gcc nowadays
thanks for your lesson
Can u make a video on linked list with single and double pointers
There's a whole course about Linked lists, I use and explain pointers and double pointers in there: code-vault.net/course/z1ry7bakhs:1603732279805
reallyyy well done!! i understood it
int main(int argc, char *argv[]) _versus_ int main(int argc, char **argv)
It's more or less the same thing. I just like using char* argv[] because it signifies that it is an array of strings in C, not just a pointer to something
@@CodeVault ..that was my point, amigo! It is the most common occurrence of this and a good example. You are correct in representing this as you do _as it makes things much clearer_ to the new C programmer.
I dropped out after one minute. Why do you use a **pointer as the function argument?
What's the issue with that?
“Why use double pointer” is at 5 mins.
First 5 mins are “what is a double pointer”.
All good.
u tried well bro
You're awesome man
Awesome!
❤️
4:34