Comparing Structure Templates and Variables

AI Thread Summary
Defining a structure template is not the same as defining a structure variable; a struct in C is simply a data type that groups various data elements, while a template in C++ allows for the creation of classes that can handle multiple data types. The discussion includes an example of a struct for a student, illustrating how it aggregates data. A user named Jessica seeks clarification on her assignment, which involves defining a structure template in C, despite the term "template" being more relevant to C++. The conversation also touches on initializing pointers and using typedef to simplify struct declarations. Overall, the thread emphasizes the differences between C structs and C++ templates while providing practical coding examples.
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
Messages
4,650
Reaction score
39
Is defining a structure template the same as defining a structure variable?
thanks.
 
Computer science news on Phys.org
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
 
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:
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;
}
 
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;
 
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:
 
Math Is Hard said:
Thanks, plover. Can I initialize theThingy the same way I initialized the structure values?
Yes, e.g.:

thingy theThingy = { 42, "Bwah ha hah!", getTopSecretData(), -0.0000000001 };
 
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:
 
Back
Top