Comparing Structure Templates and Variables

Click For Summary

Discussion Overview

The discussion revolves around the differences between defining a structure template and a structure variable in C and C++. Participants explore the concepts of structure templates, structure variables, and their initialization, particularly in the context of an assignment involving account information.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant questions whether defining a structure template is the same as defining a structure variable.
  • Another participant clarifies that a struct in C is a data type that groups different pieces of data, while a template in C++ allows for the creation of classes that can operate on any data type.
  • A participant shares their assignment details, which involve defining a structure template for account information and initializing a structure variable, expressing uncertainty about the task.
  • Code snippets are shared, showing the definition of a structure and the initialization of its variables, with one participant noting issues with initializing a pointer to another instance of the structure.
  • Another participant suggests that the term "template" may not be used in the technical C++ sense and provides an example of using typedef to simplify struct declarations.
  • Participants discuss the initialization of structure variables and pointers, with one confirming they resolved their pointer issue.
  • A light-hearted exchange occurs regarding the creativity of variable values in programming examples.

Areas of Agreement / Disagreement

Participants generally agree on the distinction between structure templates and structure variables, but there is no consensus on the use of the term "template" in the context of the assignment. The discussion remains unresolved regarding the specific interpretation of the assignment's requirements.

Contextual Notes

Participants express uncertainty about the assignment's wording and the technical definitions of templates versus structs, indicating potential limitations in understanding the task.

Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
Messages
4,663
Reaction score
36
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:
 

Similar threads

  • · Replies 26 ·
Replies
26
Views
3K
  • · Replies 56 ·
2
Replies
56
Views
4K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 5 ·
Replies
5
Views
1K
  • · Replies 50 ·
2
Replies
50
Views
6K
Replies
86
Views
4K
Replies
1
Views
2K