473,804 Members | 3,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

structure and constant memebers

hi all experts,
i have a structure with the constant memebers such as one given below:

typedef struct {
const int cbcode;
int cberror;
} xtsetplatestate rec;

now in my function i want to create a stack based object for the same:

void CreateObjectAnd UseIt{
xtsetplatestate rec plate;
xtsetplatestate rec plate2 = {0 , 0};
}
now this function is giving the compilation errors while compiling on
vc++ 6.0 since the
cbcode memeber is a constant. the error is
error C2512: 'xtsetbleedvalu esrec' : no appropriate default constructor
available
error C2552: 'tmpsetplatesta terec' : non-aggregates cannot be
initialized with initializer list

can anybody plz explain me what could be done to create instances of
these objects on stack ???
thanks
rt

Nov 15 '05 #1
13 4667

ra************@ gmail.com 写道:
hi all experts,
i have a structure with the constant memebers such as one given below:

typedef struct {
const int cbcode;
int cberror;
} xtsetplatestate rec;

now in my function i want to create a stack based object for the same:

void CreateObjectAnd UseIt{
xtsetplatestate rec plate;
xtsetplatestate rec plate2 = {0 , 0};
}
now this function is giving the compilation errors while compiling on
vc++ 6.0 since the
cbcode memeber is a constant. the error is
error C2512: 'xtsetbleedvalu esrec' : no appropriate default constructor
available
error C2552: 'tmpsetplatesta terec' : non-aggregates cannot be
initialized with initializer list

can anybody plz explain me what could be done to create instances of
these objects on stack ???
thanks
rt

why you use const there,maybe you can write like this :

typedef struct {
const int cbcode=0;
int cberror;
} xtsetplatestate rec;

Nov 15 '05 #2
ra************@ gmail.com wrote:
hi all experts,
i have a structure with the constant memebers such as one given below:

typedef struct {
const int cbcode;
int cberror;
} xtsetplatestate rec;

now in my function i want to create a stack based object for the same:

void CreateObjectAnd UseIt{
xtsetplatestate rec plate;
xtsetplatestate rec plate2 = {0 , 0};
}
The above has nothing to do with "stack based object"s. You are (quite
legally) using local variables. There is not a sign of your use of a
stack in sight.

now this function is giving the compilation errors while compiling on
vc++ 6.0 since the
cbcode memeber is a constant. the error is
error C2512: 'xtsetbleedvalu esrec' : no appropriate default constructor
available
The above error message suggests you are using a C++ compiler.
error C2552: 'tmpsetplatesta terec' : non-aggregates cannot be
initialized with initializer list
Since we can't see tmpsetplatestat erec or what you are trying to
initialize, it is impossible to tell what you are doing wrong.
can anybody plz explain me what could be done to create instances of
these objects on stack ???


If the code you are using is giving a problem (and you are confusing
local variables with "objects on stack", which means nothing in C unless
you create and maintain a stack), then stop using a C++ compiler. C and
C++ are different languages; compiling C code as if it were C++ is
asking for trouble.

If you are actually trying to modify (rather than to initialize) a const
member of a structure, then don't. Either don't modify the member or
remove the 'const' which, after all, is a lie.
Nov 15 '05 #3
us******@gmail. com wrote:
why you use const there,maybe you can write like this :

typedef struct {
const int cbcode=0;
int cberror;
} xtsetplatestate rec;


Why would you think such a foolish thing?
Nov 15 '05 #4
In article <11************ **********@g49g 2000cwa.googleg roups.com>,
<ra************ @gmail.com> wrote:
error C2512: 'xtsetbleedvalu esrec' : no appropriate default constructor
available


This appears to be a C++ error message. Probably there is some way to
persuade your compiler to compile your program as C, perhaps by giving
the file a name with a suitable extension.

-- Richard

Nov 15 '05 #5
Martin Ambuhl <ma*****@earthl ink.net> writes:
ra************@ gmail.com wrote:
hi all experts,
i have a structure with the constant memebers such as one given
below:
typedef struct {
const int cbcode;
int cberror;
} xtsetplatestate rec;
now in my function i want to create a stack based object for the
same:
void CreateObjectAnd UseIt{
xtsetplatestate rec plate;
xtsetplatestate rec plate2 = {0 , 0};
}


The above has nothing to do with "stack based object"s. You are
(quite legally) using local variables. There is not a sign of your
use of a stack in sight.


To be fair, local variables are allocated on the system stack in most
C implementations , just as malloc()-allocated objects are allocated on
the "heap". Even though the C standard doesn't use the word "stack"
or "heap" (I just checked), both terms can be convenient shorthands
for automatic and allocated storage duration, respectively.

It's worth pointing out that the standard doesn't use the term
"stack", and that not all C implementations *necessarily* use a system
stack in the commonly understood sense, but I don't see the need to
pretend we don't know what it means.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #6
Keith Thompson wrote:
It's worth pointing out that the standard doesn't use the term
"stack", and that not all C implementations *necessarily* use a system
stack in the commonly understood sense, but I don't see the need to
pretend we don't know what it means.


Since I *do* know what it means to maintain a stack in program, and I
*do* know how to store objects in such a stack, it would be wrong to
pretend that I know that the OP's use of "stack based objects" is a
mistake for "local variables." Since I more than once allowed for the
possibility that he suffered from such confusion, it would be dishonest
for someone to claim that I pretended not know what that -- of more than
one possibility -- might be what the OP poster meant. Do you purposely
misread my posts so you can play the mis-directed anti-pedant?

Nov 15 '05 #7
Martin Ambuhl <ma*****@earthl ink.net> writes:
[snip]
Do you purposely misread my posts so you can play the mis-directed
anti-pedant?


No. If I've misread your post (which is entirely possible), it's
unintentional.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #8
Martin Ambuhl <ma*****@earthl ink.net> writes:
Keith Thompson wrote:
It's worth pointing out that the standard doesn't use the term
"stack", and that not all C implementations *necessarily* use a system
stack in the commonly understood sense, but I don't see the need to
pretend we don't know what it means.


Since I *do* know what it means to maintain a stack in program, and I
*do* know how to store objects in such a stack, it would be wrong to
pretend that I know that the OP's use of "stack based objects" is a
mistake for "local variables." Since I more than once allowed for the
possibility that he suffered from such confusion, it would be
dishonest for someone to claim that I pretended not know what that --
of more than one possibility -- might be what the OP poster meant. Do
you purposely misread my posts so you can play the mis-directed
anti-pedant?


I should expand on what I wrote a moment ago.

I assumed that the OP was using the term "stack-based objects" to
refer to objects allocated locally to a function. I still think
that's likely, but I made that assumption without carefully reading
the previous articles. I also assumed that it should have been
obvious to you, and that you were being overly pedantic, an incorrect
assumption for which I apologize.

Now that I've re-read the previous article, it's less clear to me just
what the OP is asking -- especially since he seems to be using C++
rather than C.

Going off on a bit of a tangent, I realize I've never used, and rarely
seen, structs with const members. It looks like this is allowed, but
you can't provide an initial value within the struct declaration. If
I understand this correctly, a const member can be initialized by the
initialization of the enclosing object, but can't be re-assigned later
without invoking undefined behavior -- and there doesn't seem to be
any way to initialize a const member of a malloc()ed structure without
invoking UB. This would seem to make const members not very useful.
Perhaps they're allowed because it was easier than adding a rule to
forbid them. Am I missing something?

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #9
Keith Thompson <ks***@mib.or g> writes:

[snip]
Going off on a bit of a tangent, I realize I've never used, and rarely
seen, structs with const members. It looks like this is allowed, but
you can't provide an initial value within the struct declaration. If
I understand this correctly, a const member can be initialized by the
initialization of the enclosing object, but can't be re-assigned later
without invoking undefined behavior -- and there doesn't seem to be
any way to initialize a const member of a malloc()ed structure without
invoking UB. This would seem to make const members not very useful.
Perhaps they're allowed because it was easier than adding a rule to
forbid them. Am I missing something?


It is possible to construct a struct with const members in a
malloc'ed space, without UB. (Disclaimer: code has not been
compiled.)

... #includes as needed ...

struct thing {
int a;
const int b;
int c;
};

struct thing *
malloc_thing( int a, int b, int c ){
void *v;
struct thing *r;

r = v = malloc( sizeof *r );
if( !r ) exit( EXIT_FAILURE ); /* whatever... */

r->a = a;
*(int*) ((char*)v + offsetof(struct thing, b)) = b;
r->c = c;
return r;
}

Some people might squawk about the '(int*)' cast; they might want to
use

memcpy( (char*)v + offsetof(struct thing, b), &b, sizeof r->b );

instead.
Nov 15 '05 #10

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

Similar topics

3
4150
by: vk000 | last post by:
Hi, There is a constant structure with predefined fields values located in WinScard.dll. I am trying to get access to this structure values in my C# code. Is there any way I can do that? Basically I just need to obtain a memory reference like IntPtr to location of the structure in DLL, since I know the structure layout. I know that attribute works great for functions but looks like there is no instruction/attribute to associate...
1
421
by: ravinder thakur | last post by:
hi all experts, i have a structure with the constant memebers such as one given below: typedef struct { const int cbcode; int cberror; } xtsetplatestaterec;
6
4597
by: stuie_norris | last post by:
Hi Group, I am trying to define a strucutre of constants. Then switch on this structure of constant values. In the following example of my problem the code fails to compile due to: case (constants.one): In this statement, "constants.one" is not constant, but occurs in a
3
2652
by: Bob Day | last post by:
Using VS 2003, VB.NET, MSDE Is there a way to itterate through the items in a structure? Consider the structure below, which holds the name of wave files. I would like to itterate through them to confirm the wave files actually exist on the hard disk. I don't see any collection or way to do it. I would like something like this (or anything that would work): dim Constant as Structure_Prompts_Constant
2
2465
by: Narendra | last post by:
void function1(void *); void main() { int size_offset = 0; typedef struct { int a; int b; int c; char ch;
1
1573
by: Eran.Yasso | last post by:
Hi, I have a structure which has 4 memebers. I also have xls file which has 4 columns and x rows. I need to copy each column to a member in the struct. After that to add the struct to the stack and copy the next row. I already knows how to insert to stack. all i need to know is how to copy cells contetnt one by one and put it in struct members.
2
6386
by: sri | last post by:
Dear All, I am trying to inherit C Structure interface and its implementation developed by third party. I would like to provide a wrapper kind of class to my user. For instance, mapctx.h ------------- struct MAPCTX
15
3774
by: bernd | last post by:
Hi folks, a simple question for the experts, I guess. Obviously I am doing something wrong when trying to access an element of an array declared within a structure: #include <stdio.h> #include <stddef.h>
8
3254
by: Andrew Smallshaw | last post by:
I'm working on a data structure that began life as a skip list derivative, but has evolved to the point that it now only has a passing resemblance to them. Each node of this structure has a few constant size variables (ints and the like), an array holding the actual data (chars in this case) and a random number of pointers to other nodes in the structure. At the moment my code uses a fixed size array for the contents (which may or may...
0
9707
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9585
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10586
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.