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

Getting the address of an enum constant

62
hi all
when I am running the below program

#include<iostream>
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 6955
arne
315 Expert 100+
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
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 Expert 100+
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
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 Expert 100+
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
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
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 Expert 100+
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 Expert Mod 8TB
You can't, an enum defined value has no memory space, therefore it has no address.
Oct 21 '06 #10
Banfa
9,065 Expert Mod 8TB
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 Expert Mod 8TB
And finally please don't double post.
Oct 21 '06 #12
arne
315 Expert 100+
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 Expert Mod 8TB
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 Expert 100+
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
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_string (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 Expert 100+
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
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<iostream>
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 Expert 100+
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<iostream>
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
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
arne
315 Expert 100+
i.e invalid conversion from int to states.
as we can see below.
Correct: in your main waiting is a variable of type "enum states" to which you try to assign an int. That's why the compiler complains (in C the compiler will not complain, one of the subtle differences between C and C++)

q2) For the variables a, b I have declared them as of type states.
But not initialised I can able to fine the address.
Yes, exactly.

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.
The memory is allocated: you have the address :)
But there is (possibly) no meaningful value.

Which rule, you mean?
Oct 24 '06 #21
srikar
62
Correct: in your main waiting is a variable of type "enum states" to which you try to assign an int. That's why the compiler complains (in C the compiler will not complain, one of the subtle differences between C and C++)


Yes, exactly.


The memory is allocated: you have the address :)
But there is (possibly) no meaningful value.

Which rule, you mean?
Thank you very much arne, lot of my queries have been solved by ur
explanation
If I have any further can I directly post to you.

Thanks & Regards
srikar
Oct 24 '06 #22

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

Similar topics

20
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...
3
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
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...
21
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...
18
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
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...
13
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...
34
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...
3
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),
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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?

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.