473,414 Members | 1,744 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,414 software developers and data experts.

Question about struct aspect of standard

Can standard C structs have a constructor? Thanks in advance.

Jan 4 '06 #1
3 1048

<ma************@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Can standard C structs have a constructor? Thanks in advance.


Nope - if you're thinking of the C++ 'full monty' kinda thing.
Jan 4 '06 #2
ma************@gmail.com wrote:
Can standard C structs have a constructor? Thanks in advance.


C has no constructors, and no destructors either.
You can initialize a struct at the point of declaration

struct mystruct s = { 1, 42.0, "blue" };

.... or you can assign to it from another pre-initialized
struct:

const struct mystruct proto_s = { 0, 0.0, "" };
...
struct mystruct s;
...
s = proto_s;

.... or you can (manually) initialize its elements by
calling a function:

void init_mystruct(struct mystruct *);
...
struct mystruct s;
init_mystruct(&s);

.... or you can write a function that serves as a "factory
method," allocating and initializing a struct:

struct mystruct *factory( ...args... );
...
struct mystruct *sp, s;
sp = factory( ...args... );
if (sp == NULL) die();
s = *sp;

.... and there are lots more variations. However, nothing
in C will cause any of this to happen automagically; you'll
need to perform the machinations yourself.

--
Eric Sosman
es*****@acm-dot-org.invalid
Jan 4 '06 #3

<ma************@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Can standard C structs have a constructor? Thanks in advance.


The answer is 'no', but it could be a 'may be' depending upon what it is
you're trying to achieve. If it's a C++ clone, then, *not very easily* ...
even given the ol' cfront mechanisms.

So, what do you mean - *exactly*?
Jan 4 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Sims | last post by:
Hi, I have some small questions that have never been any problems, (for my compiler?), but always make me curious. So here goes... what does the standard sday about the 'if' statement? for...
15
by: christopher diggins | last post by:
I have written an article on how to do Aspect Oriented Programming in vanilla C++ (i.e. without language extensions or other tools such as AspectC++). The article is available at...
5
by: Paul F. Dietz | last post by:
Is the following legal C? struct foo; struct foo (*p); /* Pointer to array of 10 foo structures */ struct foo { int bar; int baz; }; main() { printf("%d\n", sizeof(*p)); } Paul Dietz...
6
by: Arthur J. O'Dwyer | last post by:
As far as I know, C89/C90 did not contain the now-standard offsetof() macro. Did C89 mandate that structs had to have a consistent layout? For example, consider the typical layout of the...
1
by: hunter hou | last post by:
Hello,Please look at the following code(from C++ in a nutshell) and my questions.Thanks,***Hunter... typedef void (*strproc)(const char*); void print(const char* str) { std::cout << "const...
10
by: haomiao | last post by:
I want to implement a common list that can cantain any type of data, so I declare the list as (briefly) --------------------------------------- struct list { int data_size; int node_num;...
25
by: Why Tea | last post by:
Thanks to those who have answered my original question. I thought I understood the answer and set out to write some code to prove my understanding. The code was written without any error checking....
12
by: Milux | last post by:
Hi All, This question has to do with interface design. Suppose I have an translation tool. It can translates structs from type "general" to other types and then does some processing. Example:...
4
by: hugo.arregui | last post by:
Hi! I have two struts like that: struct { int num; int num2; struct b arrayOfB; } a;
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.