473,772 Members | 2,272 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 #1
21 7018
arne
315 Recognized Expert Contributor
Actually I want to pass the address of the variables a,b,c using the pointers
a1,a2,a3 using the function fun1
The problem is that a,b, ... are no variables, but enumeration constants. Try something like
Expand|Select|Wrap|Line Numbers
  1. int a = 1000;
  2. int *a1;
  3. a1 = &a;
  4.  
This should work.
Oct 21 '06 #2
srikar
62 New Member
The problem is that a,b, ... are no variables, but enumeration constants. Try something like
Expand|Select|Wrap|Line Numbers
  1. int a = 1000;
  2. int *a1;
  3. a1 = &a;
  4.  
This should work.
Thanks for the fast reply,
Its working with int format
what about the enum, is it that enum constats cant be stored in pointers

regards
srikar
Oct 21 '06 #3
arne
315 Recognized Expert Contributor
Thanks for the fast reply,
Its working with int format
what about the enum, is it that enum constats cant be stored in pointers

regards
srikar
Enumerations simply asssociate names with integer values. Therfore, names in enumerations are very similar to constants defined using #define. The only two advantages of enums I am aware of are that
- the assignment of the values happens automatically, and
- the names may persist through to the debugger (which may help with debugging).

You can, however, define enumeration variables and access the address of them, but at the moment I have no idea what that may be good for ...
Oct 21 '06 #4
srikar
62 New Member
Enumerations simply asssociate names with integer values. Therfore, names in enumerations are very similar to constants defined using #define. The only two advantages of enums I am aware of are that
- the assignment of the values happens automatically, and
- the names may persist through to the debugger (which may help with debugging).

You can, however, define enumeration variables and access the address of them, but at the moment I have no idea what that may be good for ...
Thanks for the information given
So enum values cannot be assigned to pointer variables.
Can we make casting for enum variables to data types other than int.
regards
srikar
Oct 21 '06 #5
arne
315 Recognized Expert Contributor
Thanks for the information given
So enum values cannot be assigned to pointer variables.
Can we make casting for enum variables to data types other than int.
regards
srikar
Enumerators (enumeration constants) are no variables, they're constants. They can be used whenever you use an integral type.

How would such a "cast to data types other than int" look like?
Oct 21 '06 #6
srikar
62 New Member
Hi thanks once again.

Can we type cast a enum value with (void*) inorder to get the adress on
a 32 bit machine. Is it possible.

regards
srikar
Oct 21 '06 #7
srikar
62 New Member
hi all,
how to get the address of an enum constant.
Can we make type casting of enum constant to (void*) on 32 bit machine where
both enum and address are of same size.
please help me
Oct 21 '06 #8
arne
315 Recognized Expert Contributor
Hi thanks once again.

Can we type cast a enum value with (void*) inorder to get the adress on
a 32 bit machine. Is it possible.

regards
srikar
Sure, you can instantiate an enum (just as a struct or a union):
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. enum Num { jan, feb, mar };
  4.  
  5. int main( void )
  6. {
  7.     enum Num a;
  8.  
  9.     printf( "%p\n", &a );
  10.  
  11.     return 0;
  12. }
  13.  
But for what could you use that?
Oct 21 '06 #9
Banfa
9,065 Recognized Expert Moderator Expert
You can't, an enum defined value has no memory space, therefore it has no address.
Oct 21 '06 #10

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

Similar topics

20
4850
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
12370
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
11368
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
23611
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
18152
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
11204
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
9454
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,...
1
10038
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
9912
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...
1
7460
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
6715
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
5354
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
4007
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
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.