Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assignment of address to integer variable

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

like image 859
Computernerd Avatar asked Dec 30 '12 13:12

Computernerd


People also ask

How do you assign a variable to an integer?

You can define a variable as an integer and assign a value to it in a single declaration. For example: int age = 10; In this example, the variable named age would be defined as an integer and assigned the value of 10.

Can we assign an address to a variable?

The address of a variable may be obtained at any point in the program. A common error is to assign the address of an uninitialized variable to a pointer. The pointer value is correct, but the value it points to is not.

What is the address of an integer?

An address is a non-negative integer. Each time a program is run the variables may or may not be located in same memory locations. Each time you run the program above may or may not result in the same output.

Can we assign address to a variable in C?

When a variable is created in C, a memory address is assigned to the variable. The memory address is the location of where the variable is stored on the computer. When we assign a value to the variable, it is stored in this memory address.


2 Answers

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.

like image 63
Luchian Grigore Avatar answered Oct 23 '22 13:10

Luchian Grigore


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.

like image 30
Bart van Ingen Schenau Avatar answered Oct 23 '22 15:10

Bart van Ingen Schenau