Consider the following code snippet.
struct foo { int bar; char baz; };
This code snippet creates a foo structure that has two members bar and baz of type int and char respectively. Using structure, you can combine the data of multiple types under single variable.
Now, I can create a variable of this foo structure type as follows.
struct foo f;
Or I can create as many variables as I want.
struct foo f1; struct foo f2; struct foo f3; // OR struct foo f1, f2, f3;
This concludes the syntax of structure in C. Now, we are going to talk about the other related to structure concepts and then do composition to use separate concepts of C and programming in general together.
1. Initialize
It is recommended that when you create a structure variable, initialize it with 0 value.
struct foo f = { 0 };
Yes, you don't have to write 0 value for each member of the structure. Just write single 0 and rest will be taken care by the compiler. In this way, you're making sure that your variable is properly initialized and doesn't have any garbage value.
Or if you have, initialize the variable with initial values in sequence.
struct foo f = { 1, 'A' };
The sequence matters. Else write the name by prefixing dot (.) and then assign the value to the member of structure.
struct foo f = { .baz = 'A', .bar = 1 };
2. Get & Set
To access the member of the structure (get) and modifies (set) them, dot (.) operator is used.
struct foo f; // set f.bar = 1; f.baz = 'A'; // get printf("%d %c\n", f.bar, f.baz);
3. string as structure member
This is how we can define string as structure member.
struct foo { int bar; char baz[20]; };
But, we can not assign the value to this string member using dot(.) operator.
struct foo f; f.bar = 1; // ERROR f.baz = "hello, world";
We need to use strcpy() function from string.h header file to assign the string value to the string member of the structure.
struct foo f; f.bar = 1; strcpy(f.baz, "hello, world");
Interesting enough, if we have initial value, we can assign directly without use of strcpy() function.
struct foo f = { 1, "hello, world" }; // OR struct foo f = { .baz = "hello, world", .bar = 1 };
4. typedef
Consider the following code snippet where I'm creating few structure variables.
struct foo f1; struct foo f2; struct foo f3;
Notice, I need to type struct each time? We can eliminate by defining structure with typedef.
typedef struct { int bar; char baz; } foo;
We can now define the variable of type foo struct as follows.
foo f1; foo f2; foo f3;
5. structure pointer
You can define a pointer that points to structure e.g.
struct foo { int bar; char baz; }; struct foo f = { 1, 'A' }; // Pointer to struct struct foo *ptr = &f;
Now, you can access member of f using *ptr. But, you can't type *ptr.bar as . has high-precedence and ptr.bar will evaluate first and then * on top of the result. So, we need to type (*ptr).bar as follows.
(*ptr).bar = 3;
Or you can use arrow operator -> as follows (and this is recommended).
ptr->bar = 3;
Creating a pointer to the struct is useful when you want to pass the existing structure to the function and want to modifies the member of structure.
struct foo { int bar; char baz; }; void updateFoo(struct foo *ptr) { ptr->bar = 2; ptr->baz = 'B'; } struct foo f = { 1, 'A' }; updateFoo(&f);
6. malloc() struct
In previous section, we wrote,
struct foo { int bar; char baz; }; struct foo = { 1, 'A' }; struct foo *ptr = &f;
The above code works because we have assigned the initial value to the structure variable and memory is being allocated. But, when you don't have the initial value then you need to allocate the memory using malloc() function.
struct foo { int bar; char baz; }; struct foo *f = malloc(sizeof(struct foo));
The above code calculate the size of foo struct and allocate the memory. Then it saves the pointer to the location in f variable. After this, we can work with members of structure e.g.
struct foo { int bar; char baz; }; struct foo *f = malloc(sizeof(struct foo)); f->bar = 1; f->baz = 'A';
Once you don't with f, you must need to free the memory using free() function.
free(f);
No comments:
Post a Comment