468,504 Members | 1,968 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,504 developers. It's quick & easy.

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 5679
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,055 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,055 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,055 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,055 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

Post your reply

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

Similar topics

20 posts views Thread by Glenn Venzke | last post: by
3 posts views Thread by Ajax Chelsea | last post: by
12 posts views Thread by Steven T. Hatton | last post: by
21 posts views Thread by Bilgehan.Balban | last post: by
18 posts views Thread by Visual Systems AB \(Martin Arvidsson\) | last post: by
10 posts views Thread by kar1107 | last post: by
13 posts views Thread by toton | last post: by
34 posts views Thread by Steven Nagy | last post: by
3 posts views Thread by Gangreen | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.