424,953 Members | 1,109 Online
Need help? Post your question and get tips & solutions from a community of 424,953 IT Pros & Developers. It's quick & easy.

# difference between address and pointer

 P: n/a Hi All. I would like to know the following information. 1)Is there any difference between the address and integer ? For example suppose int x = 500; And int y =10; Suppose address of y is also 500; My doubt is there any difference between the properties of value of x and address of y which is 500. Regards, Somenath Int x =500; Aug 9 '07 #1
16 Replies

 P: n/a somenath wrote: I would like to know the following information. 1)Is there any difference between the address and integer ? Addresses (and pointers) are different from integers. For example suppose int x = 500; And int y =10; Suppose address of y is also 500; It isn't. 500 is an integer; the address of y is a pointer. They are two distinguishable types /even if/ the underlying machine uses integer-like bit-patterns as pointers and integer arithmetic to do address calculations. My doubt is there any difference between the properties of value of x and address of y which is 500. Yes. For example, it's legal to multiply an integer by another integer, but not to multiply an address by an integer. Integer values can be assigned to floating variables; addresses cannot. You cannot dereference an integer. Etc. -- Chris "almost but not quite completely different" Dollin Hewlett-Packard Limited registered office: Cain Road, Bracknell, registered no: 690597 England Berks RG12 1HN Aug 9 '07 #2

 P: n/a somenath wrote: Hi All. I would like to know the following information. 1)Is there any difference between the address and integer ? For example suppose int x = 500; And int y =10; Suppose address of y is also 500; My doubt is there any difference between the properties of value of x and address of y which is 500. This question is regularly thrashed over in the group. A little scanning in Google Groups would probably find the most recent discussion... Two points for now:- 1) The format of a pointer is not defined by the C standard 2) Not all systems have "flat" address spaces, so the idea that an address is an integer is not portable. Aug 9 '07 #3

 P: n/a somenath wrote On 08/09/07 10:12,: Hi All. I would like to know the following information. 1)Is there any difference between the address and integer ? For example suppose int x = 500; And int y =10; Suppose address of y is also 500; My doubt is there any difference between the properties of value of x and address of y which is 500. Other respondents have explained that a pointer is not an integer, by showing that an address is not an integer. But the difference goes even deeper: a pointer is not just an address. Contemplate this code fragment for a while, and you'll understand why a pointer is more than an address: union { double d; int i; } u; double *dp = &u.d; int *ip = &u.i; In this code, the two pointers dp and ip point to the same memory location. Nonetheless, they are different. What makes them different? -- Er*********@sun.com Aug 9 '07 #4

 P: n/a somenath wrote: > I would like to know the following information. 1)Is there any difference between the address and integer ? For example suppose int x = 500; And int y =10; Suppose address of y is also 500; My doubt is there any difference between the properties of value of x and address of y which is 500. Yes. You haven't completed the description of y, which also needs to specify such things as memory block, whether or not actually in memory (may be on disk, and swapped out, for example), etc. etc. POINTERS ARE NOT INTEGERS. -- Chuck F (cbfalconer at maineline dot net) Available for consulting/temporary embedded and systems. -- Posted via a free Usenet account from http://www.teranews.com Aug 9 '07 #5

 P: n/a "somenath"

 P: n/a Eric Sosman San Diego Supercomputer Center <* "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Aug 9 '07 #7

 P: n/a Eric Sosman wrote: somenath wrote On 08/09/07 10:12,: >Hi All.I would like to know the following information.1)Is there any difference between the address and integer ?For example suppose int x = 500;And int y =10;Suppose address of y is also 500;My doubt is there any difference between the properties of value of xand address of y which is 500. Other respondents have explained that a pointer is not an integer, by showing that an address is not an integer. But the difference goes even deeper: a pointer is not just an address. Contemplate this code fragment for a while, and you'll understand why a pointer is more than an address: union { double d; int i; } u; double *dp = &u.d; int *ip = &u.i; In this code, the two pointers dp and ip point to the same memory location. Nonetheless, they are different. What makes them different? They have different type. They have identical value. They have identical size (4). They cannot be distinguished from one another except by the compiler. Why do you think they are different? -- Joe Wright "Everything should be made as simple as possible, but not simpler." --- Albert Einstein --- Aug 9 '07 #8

 P: n/a Joe Wright Other respondents have explained that a pointer isnot an integer, by showing that an address is not aninteger. But the difference goes even deeper: a pointeris not just an address. Contemplate this code fragmentfor a while, and you'll understand why a pointer is morethan an address: union { double d; int i; } u; double *dp = &u.d; int *ip = &u.i;In this code, the two pointers dp and ip point to thesame memory location. Nonetheless, they are different.What makes them different? They have different type. They have identical value. They have identical size (4). They cannot be distinguished from one another except by the compiler. Why do you think they are different? You said it yourself: they have different type. Furthermore, how do you know they have the same size, or that their size is 4 bytes? I work on systems where they'd both have a size of 8 bytes, and it's entirely possible for their sizes to differ. -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <* "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Aug 9 '07 #9

 P: n/a Joe Wright wrote On 08/09/07 18:01,: Eric Sosman wrote: >>somenath wrote On 08/09/07 10:12,: >>>Hi All.I would like to know the following information.1)Is there any difference between the address and integer ?For example suppose int x = 500;And int y =10;Suppose address of y is also 500;My doubt is there any difference between the properties of value of xand address of y which is 500. Other respondents have explained that a pointer isnot an integer, by showing that an address is not aninteger. But the difference goes even deeper: a pointeris not just an address. Contemplate this code fragmentfor a while, and you'll understand why a pointer is morethan an address: union { double d; int i; } u; double *dp = &u.d; int *ip = &u.i;In this code, the two pointers dp and ip point to thesame memory location. Nonetheless, they are different.What makes them different? They have different type. They have identical value. They have identical size (4). They cannot be distinguished from one another except by the compiler. Why do you think they are different? Because `array[*ip]' might work (given a suitable value in `u.i') while `array[*dp]' won't even compile. Because `ip = dp' won't even compile. Because `sizeof *ip != sizeof *dp' (probably). Because `*dp = DBL_MAX' is legal but `*ip = DBL_MAX' probably invokes undefined behavior. Because `if ((*dp = DBL_MIN))' branches oppositely to `if ((*ip = DBL_MIN))'. Because `printf ("%f\n", *dp)' works (with a suitable value in `u.d') but `printf ("%f\n", *ip)' invokes undefined behavior, always. Because we've moved beyond B. -- Er*********@sun.com Aug 9 '07 #10

 P: n/a Eric Sosman wrote: Joe Wright wrote On 08/09/07 18:01,: >Eric Sosman wrote: >>somenath wrote On 08/09/07 10:12,:Hi All.I would like to know the following information.1)Is there any difference between the address and integer ?For example suppose int x = 500;And int y =10;Suppose address of y is also 500;My doubt is there any difference between the properties of value of xand address of y which is 500. Other respondents have explained that a pointer isnot an integer, by showing that an address is not aninteger. But the difference goes even deeper: a pointeris not just an address. Contemplate this code fragmentfor a while, and you'll understand why a pointer is morethan an address: union { double d; int i; } u; double *dp = &u.d; int *ip = &u.i;In this code, the two pointers dp and ip point to thesame memory location. Nonetheless, they are different.What makes them different? They have different type. They have identical value. They have identicalsize (4). They cannot be distinguished from one another except by thecompiler. Why do you think they are different? Because `array[*ip]' might work (given a suitable value in `u.i') while `array[*dp]' won't even compile. Because `ip = dp' won't even compile. Because `sizeof *ip != sizeof *dp' (probably). Because `*dp = DBL_MAX' is legal but `*ip = DBL_MAX' probably invokes undefined behavior. Because `if ((*dp = DBL_MIN))' branches oppositely to `if ((*ip = DBL_MIN))'. Because `printf ("%f\n", *dp)' works (with a suitable value in `u.d') but `printf ("%f\n", *ip)' invokes undefined behavior, always. Because we've moved beyond B. Perhaps I was unclear. Consider please.. void *y, *z; y = &u.d; z = &u.i; Now how shall we differentiate between y and z? -- Joe Wright "Everything should be made as simple as possible, but not simpler." --- Albert Einstein --- Aug 10 '07 #11

 P: n/a Joe Wright wrote: Eric Sosman wrote: >Joe Wright wrote On 08/09/07 18:01,: >>Eric Sosman wrote:somenath wrote On 08/09/07 10:12,:Hi All.>I would like to know the following information.>1)Is there any difference between the address and integer ?>For example suppose int x = 500;And int y =10;Suppose address of y is also 500;My doubt is there any difference between the properties of value of xand address of y which is 500. Other respondents have explained that a pointer isnot an integer, by showing that an address is not aninteger. But the difference goes even deeper: a pointeris not just an address. Contemplate this code fragmentfor a while, and you'll understand why a pointer is morethan an address: union { double d; int i; } u; double *dp = &u.d; int *ip = &u.i;In this code, the two pointers dp and ip point to thesame memory location. Nonetheless, they are different.What makes them different?They have different type. They have identical value. They haveidentical size (4). They cannot be distinguished from one anotherexcept by the compiler. Why do you think they are different? Because `array[*ip]' might work (given a suitablevalue in `u.i') while `array[*dp]' won't even compile. Because `ip = dp' won't even compile. Because `sizeof *ip != sizeof *dp' (probably). Because `*dp = DBL_MAX' is legal but `*ip = DBL_MAX'probably invokes undefined behavior. Because `if ((*dp = DBL_MIN))' branches oppositelyto `if ((*ip = DBL_MIN))'. Because `printf ("%f\n", *dp)' works (with a suitablevalue in `u.d') but `printf ("%f\n", *ip)' invokes undefinedbehavior, always. Because we've moved beyond B. Perhaps I was unclear. Consider please.. void *y, *z; y = &u.d; z = &u.i; Now how shall we differentiate between y and z? They are the same. So are `&u.i' and `(int*)&u'. But `y' and `z' are different from both `dp' and `ip'. -- Eric Sosman es*****@ieee-dot-org.invalid Aug 10 '07 #12

 P: n/a Joe Wright wrote: Eric Sosman wrote: .... snip ... >> union { double d; int i; } u; double *dp = &u.d; int *ip = &u.i;In this code, the two pointers dp and ip point to thesame memory location. Nonetheless, they are different.What makes them different? They have different type. They have identical value. They have identical size (4). They cannot be distinguished from one another except by the compiler. Why do you think they are different? And what gives you the right to make those statements? The addresses may specify all sorts of added information, such as memory bank, disk drive (for cached), type of data referenced, etc. You don't know how the system organizer has arranged all that. -- Chuck F (cbfalconer at maineline dot net) Available for consulting/temporary embedded and systems. -- Posted via a free Usenet account from http://www.teranews.com Aug 10 '07 #13

 P: n/a Keith Thompson wrote: Eric Sosman Other respondents have explained that a pointer isnot an integer, by showing that an address is not aninteger. But the difference goes even deeper: a pointeris not just an address. [...] Except that the standard tends to use the terms "pointer" and "address" more or less interchangeably. For example, the unary '&' operator yields the *address* of its operand. The term "address" is also commonly used to refer to a raw machine address (the difference is whether it simply refers to a location in memory, or also has type information associated with it). Wrong nomenclature IMO. An address is not necessarily an integer. -- Chuck F (cbfalconer at maineline dot net) Available for consulting/temporary embedded and systems. -- Posted via a free Usenet account from http://www.teranews.com Aug 10 '07 #14

 P: n/a CBFalconer Eric Sosman > Other respondents have explained that a pointer isnot an integer, by showing that an address is not aninteger. But the difference goes even deeper: a pointeris not just an address. [...]Except that the standard tends to use the terms "pointer" and"address" more or less interchangeably. For example, the unary '&'operator yields the *address* of its operand.The term "address" is also commonly used to refer to a raw machineaddress (the difference is whether it simply refers to a location inmemory, or also has type information associated with it). Wrong nomenclature IMO. An address is not necessarily an integer. Um, where did I mention integers? What I'm calling a "raw machine address" refers to a location in memory. Something integer-like is a common way to implement that, but it's not the only way (except that it's almost certainly made of bits). -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <* "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Aug 10 '07 #15

 P: n/a CBFalconer said: Joe Wright wrote: >Eric Sosman wrote: ... snip ... >>> union { double d; int i; } u; double *dp = &u.d; int *ip = &u.i;In this code, the two pointers dp and ip point to thesame memory location. Nonetheless, they are different.What makes them different? They have different type. They have identical value. They haveidentical size (4). They cannot be distinguished from one anotherexcept by the compiler. Why do you think they are different? And what gives you the right to make those statements? The addresses may specify all sorts of added information, such as memory bank, disk drive (for cached), type of data referenced, etc. You don't know how the system organizer has arranged all that. All true, but whatever information the system must store in dp to enable it to locate u.d must *also* be stored in ip to enable it to locate u.i - so as far as I can see, Joe Wright has a point. The most contentious part of his statement is that the two pointers "have identical value" - I see value as being fundamentally bound up with type, so I don't quite agree that two pointers of different type can have the same value. Nevertheless I accept that they both point to the same bunch of bits. -- Richard Heathfield Email: -www. +rjh@ Google users: "Usenet is a strange place" - dmr 29 July 1999 Aug 10 '07 #16

 P: n/a Joe Wright wrote: Eric Sosman wrote: >Joe Wright wrote On 08/09/07 18:01,: >>Eric Sosman wrote:somenath wrote On 08/09/07 10:12,: .... snip ... >>>> union { double d; int i; } u; double *dp = &u.d; int *ip = &u.i; .... snip ... > Perhaps I was unclear. Consider please.. void *y, *z; y = &u.d; z = &u.i; Now how shall we differentiate between y and z? y is a pointer to a double. z is a pointer to an int. Or rather, they are such pointers converted to void*, and can only be accurately converted back to the original types. QED. For example, dp = z; -- Posted via a free Usenet account from http://www.teranews.com Aug 10 '07 #17

### This discussion thread is closed

Replies have been disabled for this discussion.