Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C structures

  1. Aug 7, 2004 #1

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Is defining a structure template the same as defining a structure variable?
    thanks.
     
  2. jcsd
  3. Aug 7, 2004 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    I believe you mean: is a "template" the same this as a "struct?"

    The answer is no. The struct is C entity that just groups together a bunch of different pieces of data into one data type. So, for example, a Student struct my contain the name, the student ID number, the GPA, etc. It's just an agglomeration of data.

    The template, on the other hand, is a way to write a C++ class so that it can operate on any kind of data type. For example, you might write a LinkedList class. You don't want that class to just be able to store integers, or just be able to store Students -- you would write it as a template so that it can be used to store any kind of data. An integer LinkedList would be referred to as LinkedList<int>, while a LinkedList<Student> would contain Student objects. The compiler actually makes an entirely new copy of the LinkedList class for each different data type, and compiles it anew with that new data type inserted in all the appropriate places.

    Templates are syntactically very difficult; the C++ language is really pretty bastardized. You can learn about templates from any C++ textbook, or a million places on the web.

    - Warren
     
  4. Aug 7, 2004 #3

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    hmmm.. I am not sure I understand my assignment then. it says:

    Define a structure template that will contain the following data:

    a. Account number
    b. Account owner street address (string)
    c. Account owner city/state (string)
    d. Account owner zip code
    e. Account balance
    f. Account credit limit
    g. A pointer to another instance of this structure type

    Define a structure variable using this template, and initialize it. The pointer should be initialized so that it does not point to any valid address. Use printf to print the contents of all of the structure variable's members, including the pointer.


    I'll post what I have done so far and you you can see if I am on the right track.
    Thanks,
    Jessica

    p.s. we're only doing C in this class. No C++ yet.
     
    Last edited: Aug 7, 2004
  5. Aug 7, 2004 #4

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    There's a couple of lines commented out because I am still working on initializing a pointer to another instance of this structure type.
    PHP:

    #include <stdio.h>
    int main()
    {
        struct info //definition of the info structure template
        {
            int owner_account_num;
            char owner_street_address[35];
            char owner_city_state[35];
            char owner_zip[6];
            float owner_account_balance;
            float owner_credit_limit;
        //  struct info * pointer_to_instance;
        };

        struct info typical_account = {
        1001001001, "129 Maple Street","Tampa FL","90903", 7.345, 9.99
        };

        printf("%d, %s, %s, %s, %f, %f \n", typical_account.owner_account_num,
            typical_account.owner_street_address, typical_account.owner_city_state,
            typical_account.owner_zip,typical_account.owner_account_balance,
            typical_account.owner_credit_limit);
    //  printf("Pointer value: ");
      return 0;
    }
     
     
  6. Aug 7, 2004 #5

    plover

    User Avatar
    Homework Helper

    I suspect that the word template is not being used in the technical C++ sense. It looks like what you have is correct.

    As for initializing pointers to an invalid address, I recall there being a discussion of null pointers in one of your threads. (I can't seem to find it at the moment though.)

    You can also make structs slightly easier to work with using typedef:

    PHP:
    typedef struct thingy_tag {
        int someNumber;
        char* aBigOlString;
        void* secretDataNoPeeking;
        double whoPutThisHere;
    } thingy;
    Now you can just declare a variable:

    thingy theThingy;
     
  7. Aug 7, 2004 #6

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Thanks, plover. Can I initialize theThingy the same way I initialized the structure values?

    I got my pointer problem, fixed, by the way. It was just giving me trouble for some reason so I put it off til the end :biggrin:
     
  8. Aug 7, 2004 #7

    plover

    User Avatar
    Homework Helper

    Yes, e.g.:

    thingy theThingy = { 42, "Bwah ha hah!!!", getTopSecretData(), -0.0000000001 };
     
  9. Aug 7, 2004 #8

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    ha ha ha! Your variable values are much more fun than mine!
    I tried to make the next program I am working on a little more fun. It has to open and read multiple files specified from the command line so I thought I could make the file contents read like this:

    FileA: Mares eat oats,
    FileB: And does eat oats,
    FileC: And little lambs eat ivy.

    Of course I guess that's only amusing if you're familiar with the old song "Mairzy Doats"! :smile:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C structures
  1. Learning C/C++ (Replies: 5)

  2. C++ vector (Replies: 3)

  3. Learning C++ (Replies: 9)

  4. A C problem (Replies: 9)

Loading...