5

How come you can assign an address to an integer variable like this,the complier will not give an error. i always though you can only assign integer values to a integer variable

int a=0x28ff1c

You can do the same for a char variable, the complier will not give a error

char b=0x28ff1c

It will output on the console screen rubbish value for char b and a random value for int a

cout<<b
    <<endl;
cout<<a;

Can someone explain to me why there is a difference in the output for char b and int a. Can someone aslo explain to me why a char variable and integer variable can have addresses assign to it

1
  • 3
    0x28ff1c is an integer value. Commented Dec 30, 2012 at 13:45

4 Answers 4

7

0x28ff1c is not an address itself - it's just a hexadecimal number.

The following are equivalent:

int a =   2686748;  //decimal number
int a =  0x28ff1c;  //hexadecimal number
int a = 012177434;  //octal number

An address is represented by a pointer - if it's just that, an address, you can use a void*:

void* p = (void*)0x28ff1c;

In which case

int a = p;

wouldn't compile. p is an address, the number itself isn't.

Sign up to request clarification or add additional context in comments.

2 Comments

@Luchin how do you explain the difference in output of int a and char b and how come the complier doesnt give a error when assigning a integer value to char b
@Lim a char is only 1 byte, so it can't hold the whole value. A warning can be issued, but it's not required to. You're basically overflowing the char variable (so it prints an incorrect result).
5

The number 0x28ff1c is just the hexadecimal (base-16) representation of the decimal (base-10) number 2686748. As cout defaults to printing decimal values for integers, that is probably the number you got printed.

The case with char b = 0x28ff1c is slightly different, because

  1. char is not large enough to hold that value. The practical result is that it gets truncated to 0x1c.
  2. cout treats char specially, because it is normally used to hold textual data, so cout prints the character that has the code 0x1c, which is some kind of control character. You could try it with 0x41 for example (which represents 'A' in ASCII and UTF-8).

And note that there is nothing that marks 0x28ff1c as being an address. An address would be formed by &a or (void*)0x28ff1c.

2 Comments

(char*)0x28ff1c is safer (unless you can guarantee 0x28ff1c is properly aligned for int) - cc. @Lim Bonus question - is the result truncated, or is it undefined behavior because of the overflow (take into account that char is signed)?
@LuchianGrigore Implementation-defined (unless CHAR_BIT >= 23), it could saturate, truncate, map all out-of-range values to 13, the implementation just has to document what it does. In C, it could even raise an implementation-defined signal.
1

Because in any literal starting 0x is actually an integer. So it is allowed. An address is can sometimes be an integer.

12 Comments

Addresses are not integers. In many implementations, it may be possible to convert addresses to integers without issues. In some implementations, addresses may have a structure that is not well represented in a single integer (e.g., adding 1 to the encoding of an address might not produce an address that is 1 byte further in memory).
Good point @EricPostpischil. I'll say "Sometimes an address can be an integer"
@EricPostpischil I don't see how that's relevant. Granted, an int is not guaranteed to hold an address, but pointer arithmetics isn't in question here. What you pointed out (adding to an address) is much more complicated than that (it can even lead to UB).
@LuchianGrigore: It is relevant because a statement was made that is false and that fosters an incorrect mental model in people learning programming.
@EricPostpischil definitely, an address is not an integer. Well, it is, but not an int. I wasn't arguing that. Just that there are a lot of nitpicks that can't be fully described in a comment.
|
0

You can also use *(int *)(Address) = value as a construct to assigne a value to Address and use the answer by @Luchian Grigore

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.