473,787 Members | 2,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting the address of an enum constant

62 New Member
hi all
when I am running the below program

#include<iostre am>
enum one
{
a=1000,b=2000,c ,d,z};
int main()
{
one* a1;one* a2;one* a3;
a1=&a;a2=&b;a3= &c;
int fun1(one*,one*, one*);
int x;
cout<<"the starting values are"<<a<<"\t"<< b<<"\t"<<c<<"\t "<<d<<"\t"< <z;
x=fun1(a1,a2,a3 );
cout<<"the sum of the values are\t"<<d;
return 0;}
int fun1(one *a,one *b,one *c)
{
return (*b-*a);
}

I am getting the following errors
non-lvalue in unary `&'
enumptr.cpp:9: non-lvalue in unary `&'

Actually I want to pass the address of the variables a,b,c using the pointers
a1,a2,a3 using the function fun1
I am assigning the adress of a,b,c to a1,a2,a3 using "&" operator
Is it the wright way of assigning the address in C++

please help me
Oct 21 '06
21 7020
Banfa
9,065 Recognized Expert Moderator Expert
But for what could you use that?
You would use it in the same way you would use a pointer to an integer type (e.g. int *). If you want to pass an array of that type to a function or if you want a function to fill in a variable of that type.
Oct 21 '06 #11
Banfa
9,065 Recognized Expert Moderator Expert
And finally please don't double post.
Oct 21 '06 #12
arne
315 Recognized Expert Contributor
You can define a variable of type enum, but it is considered to have type int then. Of this variable you can of course take the address (as it allocates memory space, see my example above), but not of the enumeration constant itself.
Oct 21 '06 #13
Banfa
9,065 Recognized Expert Moderator Expert
You can define a variable of type enum, but it is considered to have type int then. Of this variable you can of course take the address (as it allocates memory space, see my example above), but not of the enumeration constant itself.
Agreed except that I was under the impression that it was on C where variables of type enum are considered to be int (actually I know for a fact on some compilers it is an integer type (char, short, int, long) of large enough size to hold all the values of the enum).

I thought on C++ enums where there own type, not integer types.
Oct 21 '06 #14
arne
315 Recognized Expert Contributor
Agreed except that I was under the impression that it was on C where variables of type enum are considered to be int (actually I know for a fact on some compilers it is an integer type (char, short, int, long) of large enough size to hold all the values of the enum).

I thought on C++ enums where there own type, not integer types.
Didn't know, looked it up, you are right:

"Each enumeration is a distinct type. The type of an enumerator is its enumeration. For example, AUTO is of type keyword" in

Expand|Select|Wrap|Line Numbers
  1. enum keyword{ ASM, AUTO, BREAK };
  2.  
From Stroustrup's "The C++ Programming Language". :)
Oct 22 '06 #15
srikar
62 New Member
Didn't know, looked it up, you are right:

"Each enumeration is a distinct type. The type of an enumerator is its enumeration. For example, AUTO is of type keyword" in

Expand|Select|Wrap|Line Numbers
  1. enum keyword{ ASM, AUTO, BREAK };
  2.  
From Stroustrup's "The C++ Programming Language". :)
Thank you arne & banfa for ur explanations.
Still I am having some questions in mind
When i declare the variable by using enum key word along with the varibale
I am able to get the address of the variable, but when i derefernce this address i.e by using pointer "*" to access the value of enum I am not getting the right values. I am getting some garbage values.

Is it that both are different i.e when we separetely declare the variable in main
and when it is declared with in the enum outside the main.

I am doing porting from 16bit machine to 64 bit machine.
For me I git the instances where

all_edge_enum edge_val = (all_edge_strin g (edge_name));
here all_edge_enum is enum variable i,e enum all_edge_enum.
The all_edge_string is returning (void*). So Befoe assigning the void* to enum_val I need to type cast to *(all_edge_enum *). Is this type of casting is
the correct way. Can I cast in such a way for enum.
Oct 23 '06 #16
arne
315 Recognized Expert Contributor
I am able to get the address of the variable, but when i derefernce this address i.e by using pointer "*" to access the value of enum I am not getting the right values. I am getting some garbage values.
You mean like
Expand|Select|Wrap|Line Numbers
  1. enum states{ waiting=21, running, stopped, cancelled };
  2.  
  3. int main( void )
  4. {
  5.     enum states a, b;
  6.     enum states *b_ptr = &b;
  7.  
  8.     a = waiting;    // 21
  9.     b = stopped;  // 23
  10.  
  11.     cout << *b_ptr << endl;
  12.  
  13.     return 0;
  14. }
  15.  
This gives 23 as I would expect, or didn't I understand you?
Oct 23 '06 #17
srikar
62 New Member
Thank you arne for the immediate reply.
I am getting the correct value for *b_ptr.
But If I try to get the address of waiting I am getting error that non lvalue in unary &.
If I declare the variable again in main like below. I am getting address,
But If I dereferece this address I am not getting the correct value.
I am getting garbage values.


using namespace std;
#include<iostre am>
enum states{ waiting=21, running, stopped, cancelled };

int main( void )
{
enum states waiting;
enum states a, b;
enum states *b_ptr = &b;
enum states *ptr1=&waiting;

a = waiting; // 21
b = stopped; // 23

cout << *b_ptr << endl;
cout<<"the value of waiting state is\t"<<*ptr1;
return 0;
}
Oct 24 '06 #18
arne
315 Recognized Expert Contributor
But If I try to get the address of waiting I am getting error that non lvalue in unary &.
If I declare the variable again in main like below. I am getting address,
But If I dereferece this address I am not getting the correct value.
I am getting garbage values.


using namespace std;
#include<iostre am>
enum states{ waiting=21, running, stopped, cancelled };

int main( void )
{
enum states waiting;
enum states a, b;
enum states *b_ptr = &b;
enum states *ptr1=&waiting;

a = waiting; // 21
b = stopped; // 23

cout << *b_ptr << endl;
cout<<"the value of waiting state is\t"<<*ptr1;
return 0;
}
Be careful, you're mixing things here. In your code in main 'waiting' is the name of a variable of type 'enum states'. This variable is not initialized.
'ptr' holds the adress of this (uninitialized! ) variable. If you dereference 'ptr' I would expect garbage, as I would in

Expand|Select|Wrap|Line Numbers
  1. int i;
  2. int *ptr = &i;
  3. cout << *ptr << endl;
  4.  
In addition, you have an enumeration constant, which is not related to your variable.
Oct 24 '06 #19
srikar
62 New Member
thanks arne once again.
I just want to make things clearer.
So the enum state{...} i.e waiting, stopped, etc for them they are
enum constants so for them we cant find the address.
1.If I initilise that variable in main I am getting error
i.e invalid conversion from int to states.
as we can see below.

enum states{ waiting=21, running, stopped, cancelled };
int main( void )
{
enum states waiting=10;
enum states a, b;
enum states *b_ptr = &b;
enum states *ptr1=&waiting;

a = waiting; // 21
b = stopped; // 23

cout << *b_ptr << endl;
cout<<"the value of waiting state is\t"<<*ptr1;
// cout<<"the address of running is \t"<<&running ;
return 0;
}

q2) For the variables a, b I have declared them as of type states.
But not initialised I can able to fine the address.
q3) For any variables until the variable is initialised memory will not
be alloted for the variable is it true? For any int var also before initialisation
I am able to fine the address, than what about the above rule.
Oct 24 '06 #20

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

Similar topics

20
4851
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum EnumName FirstValue = 1 SecondValue = 2 ThirdValue = 3
3
12371
by: Ajax Chelsea | last post by:
can not the "static const int" be replaced by "static enum" anywhere? is it necessary that define special initialization syntax for "static const int"?
12
3428
by: Steven T. Hatton | last post by:
Any opinions or comments on the following? I don't say it below, but I came out on the side of using enumerations over static constants. /* I'm trying to figure out the pros and cons of using static const * int class members as opposed to using enumerations to accomplish a * similar goal. The use of static constants seems more explicit and * obvious to me. Unless I assign values to the enumerators, the * compiler will do it for me....
21
6367
by: Bilgehan.Balban | last post by:
Hi, I have two different enum definitions with members of same name. The compiler complains about duplicate definitions. Is this expected behaviour? Can't I have same-named fields in different enum definitions? I think it should have been perfectly valid to do that. Its a stupid limitation to have to define disjoint enum symbol definitions. This is like having to have disjoint member names in structures.
18
11371
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
10
23615
by: kar1107 | last post by:
Hi all, Can the compiler chose the type of an enum to be signed or unsigned int? I thought it must be int; looks like it changes based on the assigned values. Below if I don't initialize FOO_STORE to be, say -10, I get a warning about unsigned comparison and I'm seeing an infinite loop. If I do initialize FOO_STORE = -10, I don't see any warnings. No infinite loop.
13
18154
by: toton | last post by:
Hi, I have some enum (enumeration ) defined in some namespace, not inside class. How to use the enum constant's in some other namespace without using the whole namespace. To say in little detail, the enum is declared as, namespace test{ enum MyEnum{ VALUE1,VALUE2 };
34
11205
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
3
2264
by: Gangreen | last post by:
I have the following enum. ** * An enum to represent types of mechanics. * * @author * @version 1.0 */ public enum Mechanics { ISOLATION(0),
0
9655
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
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
8993
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 project—planning, coding, testing, and deployment—without 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...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
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...
0
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
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
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.