Structures are a very important and useful part of C programming. They are commonly called structs, because that is how they are named in C. Structs are used to group many variable types under one name.
You define a struct as follows:
When you define a struct, you are defining your own variable type. You can declare variables with the the struct type as follows:Code:struct name { //variables go here }; //don't forget this semicolon!
It's just like you would declare an int, or any other type of variable.Code:struct name variable;
The variables in a struct are called members, look at this struct definition:
This is a very simple struct with an integer, a, and a floating point number, b. Let's say you declare the struct like this:Code:struct intfloat { int a; float b; };
To access a member from the test variable, you must use the dot operator:Code:struct intfloat test;
This sets the member a as the number 6. You can do the same thing with member b:Code:test.a=6;
This sets b as 1.5 because b is a floating point number.Code:test.b=1.5;
Typedef is a good way to save a lot of typing. It gives a data type a new name. You can do this with the struct definition:
When using a typedef, you don't need to use struct for every variable you declare, just do this:Code:typedef struct { int a; float b; } intfloat;
If you haven't used pointers before, please read this tutorial before moving on.Code:intfloat test;
When you declare structs as pointers, you have to use the arrow operator to access members. Let's assume we already defined intfloat with a typedef, just like above:
This code will print out:Code:intfloat test; test.a=5; test.b=4.3; intfloat *pointer; pointer=&test; printf("%d, %f\n", pointer->a, pointer->b);
The arrow operator is what dereferences the pointer members.Code:5, 4.300000
Here is an example in C that uses structs to show stock statistics:
When using structures, it is usually a good idea to have functions that make it easy to handle them. My setval function sets the statistics, my printv function prints out the stock statistics.Code:#include <stdio.h> typedef struct { char *name; float value; float change; } stock; stock setval(char *name, float value, float change) { stock temp; temp.name=name; temp.value=value; temp.change=change; return temp; } void printv(stock temp) { printf("%s stats:\nStock value is:%f, Stock change is:%f\n", temp.name, temp.value, temp.change); } int main() { stock google=setval("GOOG", 591.50, 2.48); printv(google); stock redhat=setval("RHT", 27.66, 0.53); printv(redhat); return 0; }
As always, feel free to post suggestions, questions, and positive feedback.
Remember, +rep is appreciated.![]()
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)
Nicely done. +rep![]()
nice .. and +rep![]()
Thank you guys
@Egz0N: You filled up my inbox by responding to all the introductory threads![]()
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)
repable. You said "Typedef is a good way to save a lot of typing." are there any negative side effects of using typedefs in terms of speed/performance/optimization/other?
Also to two important points I would like to append to your tutorial:
As an alternative to setting each member of the struct individually by using the dot operator, you can use an alternate syntax:
You can also use arrays in structs. However, they must be of defined length unless they are the last member of the struct. For example:Code:stock setval(char *name, float value, float change) { stock temp = {name, value, change}; return temp; }Code:typedef struct { //char str[] cannot go here int length; char str[]; } string;
Also, for those people who don't have a solid understanding of the difference between char *str and char str[] often run into the following problem.
Without getting into the details of pointers and rvalues and lvalues, it is hard to explain. So someone else can do that. Zeke may have in his tutorial?Code:typedef struct { int length; char str[]; } string; int main() { //I can do this char bar[] = "Hello, World"; //But I can't do this? //foo.str = "Hello, World"; //Solution memcpy(foo.str, "Hello, World", 13); return 0; }
Note: None of this code was sent through a compiler because I'm too lazy. In theory, it should work.
Last edited by John; 12-12-2009 at 03:06 PM.
thnx bro
ohh yeah
As far as I know, there are no side effects to typedef. I guess the only problem would be if you used them when they weren't appropriate. For example, if you used typedef to make numbers ints, your source code would look weird declaring everything as numbers instead of ints.
(I was going to reply when you first posted, but I forgot until now)
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks