Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I wonder what really the &a returns?

Suppose

First case

 int a;
 int *p= &a ; it works no error

Second case

 long int a;
 long int b;
 b = & a; it wont work 

Most of us say b is a variable not a pointer. But see the below.

So the question is if the &a returns the address which is an unsigned integer then why cant we assign it to a normal variable? But why only to pointers? See below

b = (unsigned int) &a ; it works after typecasting though its not practicable.

If the address is integer format then why don't the unsigned or long integers save it? I was thinking, there must be some hidden secret behind it. Could anyone reveal it? What I thought is, pointers must be doing something inside but I wonder what it would be and why a normal variable cant be.

Thanks for all your answers but the actual question what really the &a returns? Integer value or not? if it is integer number why a variable cannot hold it? long int a =65535 \valid why not int a= &b if value of address b is 65535

I'm not worried to use it as pointer, please the question is about just saving the value. Not deferencing the address. People saying 32 or 64 bit, I'm not worried about that. Why it cant save the address if address is a integer number?

I mean why cant we assign the value, I'm not saying to assign the properties of pointers to variable but just assign the value thats it

a=65535
b = a \\ works it assigns b - 65535
&a=65535
b = & a   \\ doesn't work, if address is a some integer value,why we can't store it in a variable?

take the 16 bit as example normal pointer (the address ) size is 2 bytes and variable size is 2 bytes why cant we store the address in other variable if address is integer value thats my question i find many answers like ++ it increments by 4 to pointer and value 1 to variable, not worried about that just assigning the value is more important question.

  b = & a ; address of a is 4000
  ++b ; becomes 4001 thats it,thats not a problem 
like image 812
niko Avatar asked Sep 09 '11 11:09

niko


People also ask

What song did Kanye sample for I Wonder?

It features a soulful vocal sample from the 1972 recording "My Song" by British singer-songwriter, musician, and poet Labi Siffre.

Who originally sang the song I Wonder?

"I Wonder" is a 1944 song written and originally performed by Pvt. Cecil Gant. The original version was released on the Bronze label, before Gant re-recorded it for the Gilt-Edge label in Los Angeles.

What song does Wonder by Shawn Mendes sound like?

If you like Wonder, you might also like Standing With You by Guy Sebastian and Minefields by Faouzia and the other songs below .. Wild (feat. Gary Clark Jr.)

Who sang Wonder Wonder?

"Wonder" is a song by Natalie Merchant, released in 1995 as the second single from her solo album Tigerlily. The single reached number 20 on the US Billboard Hot 100 and number 10 on the Canadian RPM Top Singles chart, outperforming her previous single "Carnival" in Canada.


4 Answers

Integers, even long int, are not always going to be the same size as a pointer. Sometimes they will be (for example, most 32-bit architectures have sizeof(int) == sizeof(void *)), sometimes they will be different (for example, some 64-bit architectures have sizeof(long) == sizeof(void *) but some do not -- Visual C++ on Windows being a prime example of a compiler where sizeof(long) != sizeof(void *)).

There's also the fact that void * is simply not the same type as long int.

Imagine a class Foo and a class Bar, defined like so:

class Foo {
   public: int a;
};

class Bar {
   public: int b;
};

It's like asking why you can't assign an instance of class Foo to a variable of type Bar -- they're not the same thing, even though in this case both Foo and Bar have the same underlying bit pattern.

like image 161
Dean Harding Avatar answered Oct 05 '22 10:10

Dean Harding


returns the address which is an unsigned integer

No, it isn't. A pointer (address) is a pointer. Period.

like image 22
Ingo Avatar answered Oct 05 '22 09:10

Ingo


You can and it was very common, it is a pertinent portability issue now though as it cannot be used on x64 platforms with a 64-bit pointer and a 32-bit integer.

It may originate from assembler usage where a register can be easily interpreted as both an integer and a pointer.

It is not overly sensible in modern usage as it can easily lead to mistakes and confusion. Type safety improvements in compiler design disallow such usage but C99 re-introduces some similar support with uintptr_t and intptr_t which are "Integer types capable of holding object pointers".

To paraphrase your question:

Why can't we assign [a pointer] to [an integer] variable?

The answer is: because it is not assembler, C and C++ are strongly typed languages.

like image 31
Steve-o Avatar answered Oct 05 '22 09:10

Steve-o


It's not specific to C or C++. This is the same in all strongly typed languages. It's to a degree even true in English. We can say that "the color of the book is blue", but not "the color of the book is wooden". A type restricts the possible values that a variable can have.

A variable int* type can only hold values that are addresses of int's, and a variable of type long int can only hold values between LONG_MIN and LONG_MAX inclusive. That's just two completely distinct sets of values.

C and C++ are not absolute. Typecasting will allow you to bypass some restrictions. E.g. (int) 3.5 tells the compiler that you want to convert the non-integer value 3.5 to an approximately-similar integer value. This works better if the two types are more similar. Other languages may not have such typecasting; there you might need to call a function instead. E.g. ROUND(3.5, INT)

like image 40
MSalters Avatar answered Oct 05 '22 09:10

MSalters