Комментарии:
Okay, that does make sense, although it's somewhat counterintuitive. BTW the program crashed at the end probably because with the for loop you've tried to access values outside of the "people" array after the i counter got to 19 or so.
ОтветитьFor me the hardest thing about C (and C++ too) is that it has a tendency to explode. It will casually allow you to do some really dumb shit, like overwriting some random part of memory
Ответитьpointer math is an anti-pattern
ОтветитьI hate pointers but I really want to learn c
ОтветитьWhat is pointer arithmetic?
ОтветитьI directly learned to "move" pointers with just using p_Person++;, surprised to find out it isn't the default case.
Ответитьso actually, there's no arithmetics at all!! 🤯
Ответитьi admit it, i just use pointers when they fix a bug in my code...
ОтветитьAll you need to do is remove the '&' at line 13.
ОтветитьIt all makes sense, you are not trying to access the next byte, you are accessing the next element in the array, p_Person stores the address of the first people element, by incrementing it by 1, you are now accessing the next element in the array, meanwhile the way you access the struct itself is again de referencing that specific Person element by doing p_Person->age.
ОтветитьDo you have a video where you talk about void pointers and pointers to pointers?
Ответитьwait isn't p_Person that's an integer so then increasing it by 1 will mean that it's gonna go to the next element of the array (if let's say p_Person is equal to 0x100 in the first iteration where were in element zero in the array, then when we increment p_Person by 1, it's gonna be equal to 0x101 or the next address which is the next element of the array since they are one line of memory (so like a sequence of addresses will reference a sequence of elements in the array in the same order) ) am pretty new to C so what i might be saying could be wrong
ОтветитьFirst rule of pointer arithmetic - don't ask about pointer arithmetic.
Ответитьi dont get it why ppl would try to add the size of struct person
if previously *p_person is already struct of person
any way i dont understand why it would multiply instead neither
What is the name of this text editor or compiler?!
ОтветитьDidn't know the compiler did this but I also never did += sizeof() stuff because I genuinely thought that when you do *struct = &struct what you have is a pointer to a pointer array which secretly point to the struct array, I would never think that the compiler would just multiply the value because that for me looked unintuitive and like a bigger secret thing for the compiler to do that the user wouldn't even think about and so did I, really enjoyed the explanation in the video.
ОтветитьThanks for the refresher. I haven't looked at C in almost 10 years.
Ответитьi have a question for you guys, especially the guy who designed the malloc function, why do malloc doesn't set all the allocated memory values to 0, it's so annoying.
ОтветитьI do like this video but at the same time I even hate...
ОтветитьFirst of all, thank you for teaching me. Isn't "p_Person += 1";" supposed to be the last line in the for loop? So that the names get assigned on pace with the ages I guess.
Ответить100 in hex 0x64, and i know you wrote for loop comparison as i < 100 only, but compiler compared it against 99 and uses jump when less or equal. How did that happen, and isn't there a assembly equivalent of just jump when less?
ОтветитьGood one.
ОтветитьWould you be able to say
person[i]->age = 0;
Or would that cause an error?
(with sarcasm in voice: ) right... now i know why i should use pointers ... but 1sec : is writing '
for (int i =0 ; i <100 ; i++){
people[i].age=0;
}'
not exactly the same result as :'
for (int i =0 ; i <100 ; i++){
p_Person->age=0;
p_Person+=1; // ++?
}'
if so not still sure what a good reason is to use them (other thand passing big stuff as parameters to functions)
Would p_Person++ do the same as p_Person += 1?
ОтветитьWow. 🔥
Ответитьwhy do you put argc and argv in the main?
ОтветитьHow to understand pointers. Eat pizza, there's a pointer for you!
Ответить.
Ответитьok but why cant w just do people[i].age = 0; in the for loop lol
ОтветитьVery very good explanation
Ответитьthankyou I was really struggling trying to use structs like objects, now I know how to use pointers :)
ОтветитьI accept the premise that the C/C++ compiler already knows you are coding a struct and calculates the memory position of the array people. if you change the value of p_Person anywhere in the code, does that mean p_Person points to that Person in the sequence? Example:
p_Person = 3 ;
p_Person -> name[0] = 0 ;
p_Person -> age = 0 ;
Does this mean that p_Person points to Person[3] in other language's notation? In essence, which array element in Person does p_Person point to?
Many thanks.
Bull Shit We can not understand you
ОтветитьGreat explanation! Subscribed! I even understood your explanation/demonstration in assembly!!
ОтветитьYeah not as advertised. This kid may think he understands pointers but his words show otherwise.
ОтветитьA pointer is memory location that points to another memory location. Done. Easy
ОтветитьPointer arithmetic is plain evil and should be banned, forever.
ОтветитьI assume that for a void pointer the compiler will not multiply.
But also this is a great example of bad implicit behavior. It's bad and the devs of the C compiler should feel bad.
I usually use an alternative method to do that:
struct Person people[100];
struct Person *p = NULL;
for(int i = 0; i < 100; i++)
{
p = &people[i];
p->age = 0;
p->name = "someone";
}
i love you thank you so much.
ОтветитьAwesome video!
ОтветитьJajjajajajajaja went to the assembly to explain the inner workings
Starts to read it like it's the alphabet
That was impressive
Why not just use the index i. people[i] would return a pointer to the person at that index in the array
ОтветитьWow! it's Amazing I did it well ! Perfect work !
Ответить