+ Reply to Thread
Results 1 to 8 of 8

Thread: Using Structures

  1. #1
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Using Structures

    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:
    Code:
    struct name {
    //variables go here
    }; //don't forget this semicolon!
    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 variable;
    It's just like you would declare an int, or any other type of variable.

    The variables in a struct are called members, look at this struct definition:
    Code:
    struct intfloat {
    int a;
    float b;
    };
    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 test;
    To access a member from the test variable, you must use the dot operator:
    Code:
    test.a=6;
    This sets the member a as the number 6. You can do the same thing with member b:
    Code:
    test.b=1.5;
    This sets b as 1.5 because b is a floating point number.

    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:
    Code:
    typedef struct {
    int a;
    float b;
    } intfloat;
    When using a typedef, you don't need to use struct for every variable you declare, just do this:
    Code:
    intfloat test;
    If you haven't used pointers before, please read this tutorial before moving on.
    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:
    Code:
    intfloat test;
    test.a=5;
    test.b=4.3;
    intfloat *pointer;
    pointer=&test;
    printf("%d, %f\n", pointer->a, pointer->b);
    This code will print out:
    Code:
    5, 4.300000
    The arrow operator is what dereferences the pointer members.

    Here is an example in C that uses structs to show 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;
    }
    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.

    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)

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,475
    Blog Entries
    75
    Rep Power
    143

    Re: Using Structures

    Nicely done. +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Join Date
    Sep 2008
    Location
    Kosovo
    Posts
    4,032
    Rep Power
    44

  5. #4
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Re: Using Structures

    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)

  6. #5
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Using Structures

    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:
    Code:
    stock setval(char *name, float value, float change) {
        stock temp = {name, value, change};
        return temp;
    }
    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:
    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.

    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;
    }
    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?

    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.

  7. #6
    stranger is offline Newbie
    Join Date
    Dec 2009
    Location
    algeria
    Posts
    3
    Rep Power
    0

    Re: Using Structures

    thnx bro

  8. #7
    Join Date
    Jan 2010
    Posts
    18
    Rep Power
    0

    Re: Using Structures

    ohh yeah

  9. #8
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Re: Using Structures

    Quote Originally Posted by John View Post
    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?
    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)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Structures.
    By ChilledOut in forum C and C++
    Replies: 2
    Last Post: 07-17-2011, 01:11 AM
  2. C++ structures
    By Junaid92 in forum C and C++
    Replies: 5
    Last Post: 05-31-2011, 04:13 PM
  3. structures in C
    By huhz in forum C and C++
    Replies: 3
    Last Post: 11-28-2010, 09:15 AM
  4. Structures
    By josh in forum C Tutorials
    Replies: 1
    Last Post: 07-20-2010, 02:22 PM
  5. Structures
    By fread in forum C and C++
    Replies: 4
    Last Post: 10-11-2009, 05:33 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts