#### Archived

This topic is now archived and is closed to further replies.

# A Few Questions

This topic is 6168 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have three questions I hope to get answers on. 1. How do I do if I want to write values by Hex or Binary form instead of the standard ten form (or whatever it is called)? I think you can do that in Assembler by writing example 0x1E03 for hex. 2. Why doesn't it work to write this in c++: int a = 10; int b[ a ]; it complains about that 'a' isn't a constant. 3. In java you have a variable called Objects that works on all variables, does that excist in c++ too? Edited by - delbogun on September 24, 2001 3:20:24 PM

##### Share on other sites
quote:
Original post by delbogun
1. How do I do if I want to write values by Hex or Binary form instead of the standard ten form (or whatever it is called)? I think you can do that in Assembler by writing example 0x1E03 for hex.

In C/C++ you write like you did: 0x1d03 for example. This way of writing hexadecimal numbers is understood by some assemblers too. Another common way to write hex-numbers for assemblers is: 034ab0h etc...
(btw, the "ten form" as you called it is called "decimal")
quote:

2. Why doesn't it work to write this in c++:

int a = 10;
int b[ a ];

it complains about that 'a' isn't a constant.

C++ doesn't support statically allocated arrays whose size isn't known at compile time. If you know that 'a' will never change, then write:
  // The 'const' keyword tells the compiler that the value// of the variable 'a' must never change.const int a = 10; int b[ a ];

Or if the value of a is not known at compile-time (eg. if it depends on user input or something), then write it like:
  int a = 10;int * b = new int[a];// In this case you must remember to delete[] 'b' once it is no longer needed.delete[] b;

quote:

quote:
3. In java you have a variable called Objects that works on all variables, does that excist in c++ too?

No, not like in Java. In C++ you have templates that can work on a specific type of objects/types.

eg. If you want a vector containing integers you declare
  #include // the std::vector template is defined here std::vector an_int_vector;// ^^ this symbol means that 'vector' is an entity declared// in the 'std' namespace.// Read about STL (Standard Template Library) in your docs.

Here an_int_vector can only contain int's, not any kind of objects/types, but you can declare another vector that contains another type and so on (you can accomplish a vector similar to Java's with (virtual) classes you write yourself, if they all inherit from the same superclass).

Edited by - Dactylos on September 24, 2001 3:37:14 PM

##### Share on other sites
i dunno about number 1 or 3, but for number 2, a isnt a constant!
to make a a constant u have3 to declare it as one.

i.e. const int a = 10;
int b[ a ];

later

##### Share on other sites
hi!

1. you can write hex like this .. DWORD x = 0x2145;

2. this declaration is for static arrays.
if you want dynamic array du this
int a = 10;
int *b;
b = new intLink

Dave007
dave007@volny.cz

##### Share on other sites
quote:
Original post by delbogun
1. How do I do if I want to write values by Hex or Binary form instead of the standard ten form (or whatever it is called)? I think you can do that in Assembler by writing example 0x1E03 for hex.

If you''re talking about instantiating a variable with a hex value, simply prefix the hex value with ''0x''. e.g.
long a = 0xffff;

You hardly ever instantiate binary variables in C/C++. The language performs automatic tranlsation from hex to binary to decimal as the context is appropriate.

Now, if you''re talking about displaying hex or binary values, you need to use manipulators . Manipulators change the numeric display mode. Search for iomanip.h and read up on the functions in it. (Any other equivalents, anyone?)

quote:
2. Why doesn''t it work to write this in c++:

int a = 10;
int b[ a ];

it complains about that ''a'' isn''t a constant.

In C/C++ you are not permitted to "statically" declare an array with a variable length. To do this you need to manipulate pointers:
int a = 10;int *b;b = new intLink



##### Share on other sites
ok, thanks a lot guys..
the reason i wanted to write in hex is because it is easier when you work with single bits, example the fourth bit should be 1. But how do you write in octal, isn''t it 8x0000 ?

##### Share on other sites
No, to write in octal prefix with a zero:

Decimal 17
Octal 021

1. 1
2. 2
frob
16
3. 3
4. 4
5. 5

• 15
• 13
• 14
• 75
• 22
• ### Forum Statistics

• Total Topics
632145
• Total Posts
3004344

×