Struct Association

This is a discussion on Struct Association within the C Programming forums, part of the General Programming Boards category; Hey everyone! I have a question about associating 2 structs. lets say... Code: struct cust { char name[32]; char address[128]; ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    7

    Exclamation Struct Association

    Hey everyone!
    I have a question about associating 2 structs.
    lets say...

    Code:
    struct cust {
    	char name[32];
    	char address[128];
    	int id;
    } customer[50];
    
    struct products {
    	char description[128];
    	float price;
    	char id[12];
    } shop_basket[5];
    I want to associate both structs in order to have at most 50 customers buying at most 5 products each.

    Thanks in advance!

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Then each customer is going to need there own struct products array. Which is silly since you wouldn't want to keep a copy of the products in each customer.

    Store the products in an array of their own, and then an array of pointers in struct cust.

    Using both ideas:
    Code:
    struct product_s
    {
       ...
    }; // make the array of products
    
    struct basketItem_s
    {
       struct product_s * productInfo;
       int quantity;
    };
    
    struct customer_s
    {
       char name[32];
       struct basketItem_s basket[5];
    }; // array of customers
    Last edited by zacs7; 10-01-2008 at 07:02 AM.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I would think you should nest them like this:

    Code:
    struct product {
    	char description[128];
    	float price;
    	char id[12];
    };
    
    struct cust {
    	char name[32];
    	char address[128];
    	int id;
            struct product bought[5];
    } customer[50];
    So you now have, for example, customer[7].bought[2].price referring to the cost of the 2nd product for customer 7...if each product also has it's own struct (declared "struct product sparetire"), you just do this to create the bought products:

    Code:
    customer[7].bought[2] = sparetire;
    Last edited by MK27; 10-01-2008 at 07:10 AM.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Except with that idea MK27, for each customer you have to make / copy the products when they're bought.

    For example, customer A buys item Z
    Z's description, id & price are copied into A's bought[0]

    customer B also buys item Z
    Z's description, id & price are copied into B's bought[0]

    Item Z now exists in 2 (+1 if you have a list of "items") places. Waste of memory for one...

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by zacs7 View Post
    Except with that idea MK27, for each customer you have to make / copy the products when they're bought.
    I thought of that right after I looked at your addition to your first post and added a solution to my post, which I think is better than yours...

    re: memory: I guess make "bought" a pointer. The syntax for that would be:

    Code:
    struct cust {
    	char name[32];
    	char address[128];
    	int id;
            struct product *bought[5];
    } customer[50];
    and the syntax to add a purchase:

    customer[7].bought[2]=&sparetire;

    and the syntax to access an element of a "bought":

    (*customer[7].bought[2]).price
    Last edited by MK27; 10-01-2008 at 07:49 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    7

    Thumbs up

    Thanks Zacs7, MK27.

    It's for school work purposes.
    Both solutions worked great for me.

    Thank you!

  7. #7
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    MK27: You would still need the third struct (basketItem_s) if you wanted to store the quantity. It could also be expanded to work as a node on a linked list, which would allow users to buy as many different items as they want, but that would make it a bit more complex.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Remember, the disadvantage of the pointer method is that you can't give customer[7] a special price on a sparetire, because if you modify the pointer element (*customer[7].bought[2]).price you will really be modifying sparetire.price.

    mike_g: well, I thought the assignment was for "50 customers buying at most 5 products each". bought[0] and bought[1] could both point to sparetire.
    Last edited by MK27; 10-01-2008 at 08:04 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    A discount value could be added as a field to the third struct. I dont see any disadvantage to using pointers here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 07:13 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21