Comparing Structure Templates and Variables

In summary, defining a structure template is not the same as defining a structure variable. A structure is a C entity that groups together various pieces of data, while a template is used to write a C++ class that can operate on any type of data. The compiler creates a new copy of the class for each data type used. In terms of the assignment, it seems that the word "template" is not being used in the C++ sense and the provided code is on the right track. The pointer can be initialized to an invalid address using null pointers or typedef can be used to make structs easier to work with.
  • #1
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
4,652
37
Is defining a structure template the same as defining a structure variable?
thanks.
 
Computer science news on Phys.org
  • #2
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
 
  • #3
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:
  • #4
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;
}
 
  • #5
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;
 
  • #6
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:
 
  • #7
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 };
 
  • #8
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:
 

1. What is the purpose of comparing structure templates and variables?

The purpose of comparing structure templates and variables is to understand the similarities and differences between these two concepts in order to use them effectively in scientific research and data analysis. This comparison allows scientists to identify the most appropriate approach for organizing and manipulating data in their specific research studies.

2. What is a structure template in scientific research?

A structure template is a predefined format or framework used to organize and store data in a particular way. It typically includes fields or variables that can hold different types of data, such as numerical values, text, or images.

3. How do variables differ from structure templates?

Variables are placeholders for data values that can change or vary in a scientific study. They can be manipulated and analyzed to understand the relationships between different types of data. Structure templates, on the other hand, provide a framework for organizing and storing variables in a specific format.

4. Can variables be used without a structure template?

Yes, variables can be used without a structure template, but this may lead to disorganized and inconsistent data. Using a structure template provides a standardized format for storing and manipulating variables, making it easier to analyze and interpret data.

5. How do scientists determine the most suitable structure template for their research?

Scientists determine the most suitable structure template for their research by considering the type and complexity of their data, the research objectives, and the available tools and software. They may also consult with other researchers or refer to previous studies to identify the most effective structure template for their specific research project.

Similar threads

Replies
26
Views
2K
  • Programming and Computer Science
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
733
Replies
24
Views
2K
  • Quantum Physics
Replies
4
Views
535
Replies
4
Views
955
  • Quantum Interpretations and Foundations
2
Replies
50
Views
5K
  • Mechanical Engineering
Replies
12
Views
1K
Replies
4
Views
727
  • Special and General Relativity
Replies
29
Views
1K
Back
Top