0
Anyone give explanation for structures and pointers in c++??
structure and pointer in c++
2 Answers
+ 2
A structure in C++ is the same as a class. C++ has two different names for this because structs were a thing in C and C++ wanted to be backwards-compatible.
A small difference is that structs are public-by-default (again because of C), but you can of course override that like in any class.
class X{
int foo; // this is private
}
struct X{
int foo; // this is public
}
Pointers are hard when you first learn about them, but it helps to imagine your computer's memory as a big, numbered list. Any variable you use lives somewhere in that list and therefor has a memory address. Pointers are just variables that hold memory addresses.
int x = 4;
int* pointer = &x; // int* means pointer to int, &x means, get the memory address of x. Let's say thismemory address is 1234.
*pointer = 5; // That means, set whatever is behind memory address 1234 to 5.
cout << x; // Prints 5, because "x" is at address 1234 and we just changed that
To fully understand pointers you just have to read through some tutorials and test them out for a while, a comment on sololearn won't be enough :P
0
Thanks buddy