473,386 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Structs in C

Hi,
I am trying to do the following:
Expand|Select|Wrap|Line Numbers
  1. typedef struct _STRUCT1
  2. {
  3.         integer var1[SIZE];
  4.  
  5. } STRUCT1
  6.  
  7. typedef struct _STRUCT2
  8. {
  9.        STRUCT1 var2(100);
  10.        STRUCT1 var3(200);
  11. } STRUCT2
I want that var2.var1 will have the array size=100
and var3.var1 will have the array size=200;
is it posible, or must i do this:

Expand|Select|Wrap|Line Numbers
  1. typedef struct _STRUCT1
  2. {
  3.         integer var1[100];
  4.  
  5. } STRUCT1
  6.  
  7. typedef struct _STRUCT3
  8. {
  9.         integer var1[200];
  10.  
  11. } STRUCT3
  12.  
  13. typedef struct _STRUCT2
  14. {
  15.        STRUCT1 var2;
  16.        STRUCT3 var3;
  17. } STRUCT2
Oct 19 '06 #1
3 1193
pedz
4
Someone please check my syntax but something like:

Expand|Select|Wrap|Line Numbers
  1. template <int size>
  2. struct _STRUCT1
  3. {
  4.     integer var1[size];
  5. };
Now you can do a couple of things. One is:
Expand|Select|Wrap|Line Numbers
  1. typedef _STRUCT1<100> s100;
  2. typedef _STRUCT1<200> s200;
  3.  
  4. struct foo {
  5.     s100 s1;
  6.     s200 s2;
  7. };
or you can put the use of the template inside the struct like:
Expand|Select|Wrap|Line Numbers
  1. struct foo {
  2.     _STRUCT1<100> s1;
  3.     _STRUCT1<200> s2;
  4. };
Oct 19 '06 #2
templates in C?
Oct 19 '06 #3
Banfa
9,065 Expert Mod 8TB
templates in C?
No, I suspect pedz was thinking in C++.

"Think in C" to paraphase Firefox.

Anyway you can't do what you have as your first example you will have to do what you gave as your second example or alternatively put the arrays directly into STRUCT2

Expand|Select|Wrap|Line Numbers
  1. typedef struct _STRUCT2
  2. {
  3.        int var1[100];
  4.        int var2[200];
  5. } STRUCT2
  6.  
Oct 19 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: news.microsoft.com | last post by:
Hi, I am using structs and am also using property accessors to access those private member fields... TO me this is a good way of handling them, but I find alot of people using direct access to...
6
by: James Pascoe | last post by:
Dear All, Apologies if this is OT. I have a C program which processes an arbitrary number of structs that are stored in a hash table. (The nature of the processing and the layout of the...
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
10
by: Angel | last post by:
I'm using several C functions (in a dll) that receive a struct as parameter. Since I'm doing it in C#, I assume I need to recreate the struct in C# in order to call the function with the required...
5
by: Bidule | last post by:
Hi, I'm trying to sort structs defined as follows: struct combinationRec { float score; char* name; }; The number of structs and the length of the "name" field are not known
5
by: Bilgehan.Balban | last post by:
Hi, I am currently brushing up my c++ knowledge and I would like to ask you about the differences between classes and C structs, in the function/method perspective. 1) Is it correct to say...
61
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
11
by: Cliff Martin | last post by:
Hi, I am reading a fairly large file a line at a time, doing some processing, and filtering out bits of the line. I am storing the interesting information in a struct and then printing it out....
29
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much...
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.