#Topic: Pointer to a Structure.
31 messages · Page 1 of 1 (latest)
When your question is answered use !solved or the button below to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
struct something{
int a{};
};
auto main() -> int {
something your_struct;
something* pointer_to_your_struct{&your_struct};
}
?
Why do people try to explain things to newbies with the weird syntax like auto-> and { } for basic initialization
you just not need focus on it
it's unintuitive syntax or else it would have been the syntax from the start
do you understand a pointer to an int
member access operator?
this is struct to pointer?
what is this a{}?
point to an address of a variable.
Do you understand pointer to int?
int is written in that struct. Might be access to that variable 'a'. using pointer p->a something like this
p->a is shorthand for (*p).a
yes
so what specificly you are asking
understanding pointer to structure
pointer_to_your_struct points to your_struct
what's the dynamic allocation to access struct using pointer?
"In point what do mean: "this-> ""
You didn't answer the question
No, I didn't
.
a pointer just holds a memory address, and then you dereference that memory address to view the value
structs in cpp are a chunk of memory (which isn't as nice as C because there can be padding, so no navigation by offsets), but there is NEVER padding at the beginning of the struct, so a pointer to a struct is a pointer to its first element
so if I have a struct called Person with 3 elements string name, int age, string birthday, a pointer to this struct would be a pointer to name
if you don't understand pointer to int how can you understand pointer to struct
I understand that syntax that was accessing struct using pointer
Did this a small practice
int main()
cout << "accessing object dynamic allocation" << endl;
struct Rectangle *p;
p = new Rectangle;
cout << p->length << endl;
cout << p->breadth << endl;
p->length = 10, p->breadth = 6;
cout << p->length << endl;
cout << p->breadth << endl;
return 0;
Output shows
0
0
10
6
!solved