![]() |
|
[C] Structures - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.li/Forum-C-C-Obj-C) +--- Thread: [C] Structures (/Thread-C-Structures) |
[C] Structures - TheUninvited_mybb_import15721 - 03-14-2014 C structures are special, large variables which contain several named variables inside. Structures are the basic foundation for objects and classes in C++. Structures are used for: Serialization of data - Passing multiple arguments in and out of functions through a single argument - Data structures such as linked lists, binary trees, and more - The most basic example of structures are points, which are a single entity that contains two variables - x and y. Let's define a point in a two-dimensional point: Code: struct point
{
int x;
int y;
}Code: /* draws a point at 10, 5 */
int x = 10;
int y = 5;
draw(x, y);Code: /* draws a point at 10, 5 */
struct point p;
p.x = 10;
p.y = 5;
draw(p);Typedefs Typedefs allow us to define types with a different name - which can come in handy when dealing with structs and pointers. In this case, we'd want to get rid of the long definition of a point structure. We can use the following syntax to remove the struct keyword from each time we want to define a new point: Code: typedef struct
{
int x;
int y;
} point;Code: point p;Structures can also hold pointers - which allows them to hold strings, or pointers to other structures as well - which is their real power. For example, we can define a vehicle structure in the following manner: Code: typedef struct
{
char * brand;
year model;
} vehicle;Code: vehicle mycar;
mycar.brand = "Ford";
mycar.model = 2007;RE: [C] Structures - ArkPhaze - 03-15-2014 I like C++ better for things like this.
RE: [C] Structures - TheUninvited_mybb_import15721 - 03-15-2014 (03-15-2014, 12:19 AM)ArkPhaze Wrote: I like C++ better for things like this. I do also but i wanted to make a thread about it
RE: [C] Structures - Deque - 03-15-2014 Thanks for your share. Maybe you should add unions as well. What does structs have to do with serizalization? The term doesn't seem right to me in this context. RE: [C] Structures - TheUninvited_mybb_import15721 - 03-15-2014 (03-15-2014, 01:10 PM)Deque Wrote: Thanks for your share. serilization is just something i kinda wanted to introduce again when making this like you said in my lexical analysis i seem to write post's presuming people know some stuff before reading my threads, i introduced it because i thought it was a cool way to explain them RE: [C] Structures - ArkPhaze - 03-15-2014 (03-15-2014, 05:44 PM)TheUninvited Wrote:(03-15-2014, 01:10 PM)Deque Wrote: Thanks for your share. I didn't look at this or read it entirely, but the reason I mentioned C++ is because of a feature. This still has nothing to do with serialization though, and although it could be, you didn't explain anything about serialization with such types defined within a struct. Deque is right, "Serialization of data" is out of context by far. |