Thread: creating structures

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    creating structures

    Hello I was wondering if it was possible to to have user defined structures. What I exactly mean by that is if I aks the user to enter in a number and he/she enter's say 4. 4 structures containing the same type of data created but they will have different name.

    for example
    Code:
    type def {
                     int variableA;
    }struct_1; 
    
    type def {
                     int variableA;
    }struct_2; 
    
    type def {
                     int variableA;
    }struct_3; 
    
    type def {
                     int variableA;
    }struct_4;
    Is anything like this possible if it is can someone point out to me on how to do it. I don't want the answer exactly as I want to learn it myself but I do want some hints and tips to point me in the right direction.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't create new datatypes at run time. If you want the same things, then you're just looking for a "dynamic array"-type thing.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What is the ACTUAL problem you are trying to solve. This is another case of "asking a question about the details of the solution, whilst missing the overall goal" - like my example of the man who walks up to a car-mechanic at a garage asking how to loosen a wheelnut, but is actually trying to fix a puncture.

    C is a compiled language, so you can't add elements of source-code after the compile-process [1], so exactly what you describe certainly won't work. I also don't quite understand why you want to have four structures with exactly the same conent - why can't you just use the one structure?

    [1] Unless you write code hat does something similar to the compilation process.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    92
    The basic problem that I am trying to solve is that the user must enter the number of coustomers during run time. Each coustomer will have the same set of data (e.g Account Number, Bank Balance,Interest Paid). Although the amount will be different for each coustomer but each coustomer will have an account number, bank balance and interest paid. I thought that structures were the best way to go about doing them.

    Firstly is it possible to achieve something like this in C?

    If so how do I achieve this? (I don't want to answer as I want to learn but I just want some hints and tips).

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So you don't want different types of objects (which is what your original question asked about) -- you want an array of objects. If you're willing and able to use C99, you can use variable-sized arrays; otherwise you need a dynamically-allocated array.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So you have
    Code:
    struct customer {
      char name[100];
      // etc
    };
    Then you input how many you need
    int nCustomers = 4;

    Then you create them with
    struct customer *customers = malloc ( nCustomers * sizeof *customers );

    customers[0].name through customers[3] etc etc
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    92
    Thanks guys I think I know what I have to do now. Also any idea where I can find a similar example to what "Salem" was talking about so I can learn it?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can search for "struct" or "structure" in the forum.

    This one looked right for what you want:
    http://cboard.cprogramming.com/showthread.php?t=111524

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Help needed with structures
    By esmeco in forum C Programming
    Replies: 7
    Last Post: 03-08-2008, 01:29 PM
  3. creating structures in functions
    By Marksman in forum C Programming
    Replies: 3
    Last Post: 06-01-2005, 10:56 AM
  4. creating structures help
    By bobnet in forum C++ Programming
    Replies: 4
    Last Post: 08-25-2003, 08:46 PM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM