+ 6

How to correctly assign a variable by derefencing a pointer to a dynamically allocated C struct?

... if this is possible: "Under the hood, C is still using pointers to accomplish this." ( source: https://gribblelab.org/CBootCamp/8_Pointers.html#org0f30681 ) When I get the pointer to a struct firstly defined as a simple variable, I'm able to access struct members for read and write operations, either by using the variable and dot notation ( var.property ) or the pointer and the arrow notation ( ptr->property ). But doing the converse ( dynamically allocating memory to a struct pointer and derefenrencing it to a simple variable ) doesn't let me able to modify ( write ) a property of the original struct ( future access by the var.property get the new value, but keep the old value by accessing it through the ptr->property ). Also, the &var adress appears totally different of the ptr one ^^ ( sounds like a binded variable, wich store elsewhere new value, but kept reference to the original dynamically allocated struct in memory and not allowing modifications on the original struct pointer properties but only through the original ptr->properties wich doesnt reflect modifications done through the var.properties notations ). Look ( and run ) this basic code example to better figure my problem: https://code.sololearn.com/cn2KDONtLWR7/#c

11th Jun 2020, 9:56 PM
visph
visph - avatar
4 odpowiedzi
+ 5
When you dereference ptr and assign it to dat, the struct is shallow copied from ptr to dat. After the assignment dat will hold a separate copy of the struct, not a reference to it. To get a reference you'd need dat to be a pointer alias to ptr. And you'd have to dereference the dat pointer to get the underlying struct. Any mutations done by either pointer will affect the underlying memory. C doesn't have references like C++. C++ references are special pointers (hidden from you by the language) which are implicitly in dereference mode.
11th Jun 2020, 10:31 PM
Gen2oo
Gen2oo - avatar
+ 5
Yes: that's what I understood from my investigations... The "pointer alias" about wich you're talking is just another pointer isn't it? ...so there's no way to use the dot notation for a dynamically allocated struct? nor to use square bracket indexes notation for a dynamically allocated (or even maybe not dynamically allocated) array of structs? If it is, that's sadly... but not a big issue since we are aware of that limitation (I wasn't able to find reference about that around my internet researches before posting here ^^)
11th Jun 2020, 10:41 PM
visph
visph - avatar
+ 5
Visph "pointer alias" about wich you're talking is just another pointer isn't it?" Yes that is correct. You can use dot notation but it would require you to dereference the pointer first. Arrow operators like ptr->num is short hand for: (*ptr).num You can use array subscripts [] for dynamically allocated arrays of structs since arrays are pointers to the base address of a block of memory. For arrays you can also access the struct members using dot notation because array subscripts is a syntactic replacement for *(ptr + offset).
11th Jun 2020, 11:03 PM
Gen2oo
Gen2oo - avatar
+ 4
Yes, indexing works for reading, but not for writting, and since the properties are not modified ;) Anyway I know about the shorthand, and most of the few times where I code in C I prefer using the arrow notation and do pointer arithmetic by myself... I was just facing a context where I would have prefer to not :P (However, I've modified my way and successfully solutioned the codewars challenge, even if I discover after that could be done with a very more clever solution without struct at all :D -- that's the good point of learning by trying to solve and study others code solutions ;))
11th Jun 2020, 11:13 PM
visph
visph - avatar