#Topic: Pointer to a Structure.

31 messages · Page 1 of 1 (latest)

timid python
#

I need to learn and understand the concept pointer to a structure.

pearl torrentBOT
#

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.

light flint
#
struct something{
   int a{};
};

auto main() -> int {
   something your_struct;
   something* pointer_to_your_struct{&your_struct};
}

?

sturdy galleon
light flint
#

you just not need focus on it

sturdy galleon
#

it's unintuitive syntax or else it would have been the syntax from the start

sturdy galleon
timid python
light flint
#

dont worry about it

#

for now

#

do you understand what is pointer in general

timid python
sturdy galleon
#

Do you understand pointer to int?

timid python
timid python
light flint
#

so what specificly you are asking

timid python
light flint
timid python
#

what's the dynamic allocation to access struct using pointer?

stiff thicket
#

"In point what do mean: "this-> ""

sturdy galleon
timid python
stiff thicket
true jungle
# timid python 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

sturdy galleon
timid python
#

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

timid python
#

!solved