+ Reply to Thread
Results 1 to 8 of 8

Thread: Using Structures

  1. #1
    Code Warrior Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest's Avatar
    Join Date
    Sep 2009
    Location
    United States
    Age
    16
    Posts
    2,646
    Blog Entries
    3

    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.
    Code:
     / ___|_   _  ___  ___| |_ 
    | |  _| | | |/ _ \/ __| __|
    | |_| | |_| |  __/\__ \ |_ 
     \____|\__,_|\___||___/\__|

  2. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,693
    Blog Entries
    57

    Re: Using Structures

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

  3. #3
    Code Warrior Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N is a name known to all Egz0N's Avatar
    Join Date
    Sep 2008
    Location
    Kosovo
    Age
    18
    Posts
    4,034

    Re: Using Structures

    nice .. and +rep

  4. #4
    Code Warrior Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest's Avatar
    Join Date
    Sep 2009
    Location
    United States
    Age
    16
    Posts
    2,646
    Blog Entries
    3

    Re: Using Structures

    Thank you guys
    @Egz0N: You filled up my inbox by responding to all the introductory threads
    Code:
     / ___|_   _  ___  ___| |_ 
    | |  _| | | |/ _ \/ __| __|
    | |_| | |_| |  __/\__ \ |_ 
     \____|\__,_|\___||___/\__|

  5. #5
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,885
    Blog Entries
    25

    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 05:06 PM.

  6. #6
    Newbie stranger is an unknown quantity at this point
    Join Date
    Dec 2009
    Location
    algeria
    Posts
    1

    Re: Using Structures

    thnx bro

  7. #7
    Newbie LuiDaHottest is an unknown quantity at this point LuiDaHottest's Avatar
    Join Date
    Jan 2010
    Age
    20
    Posts
    18

    Re: Using Structures

    ohh yeah

  8. #8
    Code Warrior Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest's Avatar
    Join Date
    Sep 2009
    Location
    United States
    Age
    16
    Posts
    2,646
    Blog Entries
    3

    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 )
    Code:
     / ___|_   _  ___  ___| |_ 
    | |  _| | | |/ _ \/ __| __|
    | |_| | |_| |  __/\__ \ |_ 
     \____|\__,_|\___||___/\__|

+ 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. Replies: 2
    Last Post: 11-02-2009, 08:01 AM
  2. What is the point of structures?
    By Guest in forum C and C++
    Replies: 8
    Last Post: 09-30-2009, 12:08 PM
  3. SQL Decision Structures
    By chili5 in forum Tutorials
    Replies: 2
    Last Post: 09-05-2009, 09:01 AM
  4. Help with C structures
    By J-Camz in forum C and C++
    Replies: 2
    Last Post: 01-25-2009, 08:15 PM
  5. Good Data Structures Book
    By gammaman in forum General Programming
    Replies: 4
    Last Post: 10-28-2008, 10:52 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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