![]() |
pointer q
A recent post of mine showed a sufficiently large gaffe on pointers as to
need to return to K&R 5.1-6 appendix A8.6.1 . So we have type specifiers: int long .. One dreams himself variable names: qwe, qwr, writes int qwe; long qwr; and thinks he knows what types he's declared. Had you asked me about: int *qwe; long *qwr; I would have said the pointers declared were--while pointng to different types--of the same type. Why am I wrong? Joe |
Re: pointer q
"Joe Smith" <grumpy196884@netzero.net> writes:
>Had you asked me about: > > int *qwe; > long *qwr; > > I would have said the pointers declared were--while pointng to different > types--of the same type. Why am I wrong? "qwe" is of type "pointer to int". "qwr" is of type "pointer to long". Those are different types. -- "If I've told you once, I've told you LLONG_MAX times not to exaggerate." --Jack Klein |
Re: pointer q
"Joe Smith" <grumpy196884@netzero.net> writes:
> A recent post of mine showed a sufficiently large gaffe on pointers as to > need to return to K&R 5.1-6 appendix A8.6.1 . So we have type specifiers: > int > long > . One dreams himself variable names: qwe, qwr, writes > > int qwe; > long qwr; > > and thinks he knows what types he's declared. Had you asked me about: > > int *qwe; > long *qwr; > > I would have said the pointers declared were--while pointng to different > types--of the same type. Why am I wrong? Joe int* and long* are two different types (both pointer types). I'm curious, what led you to think that they're the same type? -- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> We must do something. This is something. Therefore, we must do this. |
Re: pointer q
"Keith Thompson" <kst-u@mib.org> wrote in message news:lnlkt5bqv3.fsf@nuthaus.mib.org... > "Joe Smith" <grumpy196884@netzero.net> writes: >> A recent post of mine showed a sufficiently large gaffe on pointers as to >> need to return to K&R 5.1-6 appendix A8.6.1 . So we have type >> specifiers: >> int >> long >> . One dreams himself variable names: qwe, qwr, writes >> >> int qwe; >> long qwr; >> >> and thinks he knows what types he's declared. Had you asked me about: >> >> int *qwe; >> long *qwr; >> >> I would have said the pointers declared were--while pointng to different >> types--of the same type. Why am I wrong? Joe > > int* and long* are two different types (both pointer types). > > I'm curious, what led you to think that they're the same type? What's to stop me from swapping an int * and a long *? Joe |
Re: pointer q
"Joe Smith" <grumpy196884@netzero.net> writes:
> "Keith Thompson" <kst-u@mib.org> wrote in message > news:lnlkt5bqv3.fsf@nuthaus.mib.org... >> "Joe Smith" <grumpy196884@netzero.net> writes: >>> A recent post of mine showed a sufficiently large gaffe on pointers as to >>> need to return to K&R 5.1-6 appendix A8.6.1 . So we have type >>> specifiers: >>> int >>> long >>> . One dreams himself variable names: qwe, qwr, writes >>> >>> int qwe; >>> long qwr; >>> >>> and thinks he knows what types he's declared. Had you asked me about: >>> >>> int *qwe; >>> long *qwr; >>> >>> I would have said the pointers declared were--while pointng to different >>> types--of the same type. Why am I wrong? Joe >> >> int* and long* are two different types (both pointer types). >> >> I'm curious, what led you to think that they're the same type? > > What's to stop me from swapping an int * and a long *? Joe Um, they're different types. Given: int *ip; long *lp; these assignments: ip = lp; lp = ip; are both illegal (constraint violations requiring diagnostics). You can do the assignments with explicit conversion (casts), but there's no guarantee that the results will be meaningful. -- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> We must do something. This is something. Therefore, we must do this. |
Re: pointer q
Joe Smith wrote:
> > What's to stop me from swapping an int * and a long *? Joe The type system. Of course, it's not there only to annoy you, but has very real, objective reasons to stop you from doing so. Probably the most obvious reason is the fact that the compiler needs to know what the pointer points in order to dereference it (in other words, access the value of the object it points to). Different types have different sizes and since pointer values are actually addresses of objects, the compiler needs to know how many bits/bytes it has to read from that address to retrieve the whole object, and only the object. Pointer arithmetic also depends on what the pointer object points to. Say you want to iterate over an array of int, with an int *: int foo[10], *bar; bar = &foo[0]; for(i = 0; i < 10; i++) bar++; How does the compiler know how much it needs to increment the value of bar so it points to the next element of foo? It is obvious that using a long * would advance it too much if sizeof (long) != sizeof (int). Imagine a function expecting an int * as its argument and doing something with the object it points to. Also imagine what would happen if you passed to it the wrong type of pointer. Also note that on some implementations the size of different pointer types are not the same. In short, it's not a logical thing to do. I think your confusion stems from the (incorrect) assumption that the term 'pointer' denotes a single kind of abstract type that 'points' regardless of what it points /to/. So, to summarize, the type of the object the pointers points to *is* the part of the pointer type. You cannot, therefore, exchange int * and long * freely without casts. -- Denis |
Re: pointer q
Joe Smith wrote:
> "Keith Thompson" <kst-u@mib.org> wrote in message > news:lnlkt5bqv3.fsf@nuthaus.mib.org... > >>"Joe Smith" <grumpy196884@netzero.net> writes: >> >>>A recent post of mine showed a sufficiently large gaffe on pointers as to >>>need to return to K&R 5.1-6 appendix A8.6.1 . So we have type >>>specifiers: >>>int >>>long >>>. One dreams himself variable names: qwe, qwr, writes >>> >>>int qwe; >>>long qwr; >>> >>>and thinks he knows what types he's declared. Had you asked me about: >>> >>>int *qwe; >>>long *qwr; >>> >>>I would have said the pointers declared were--while pointng to different >>>types--of the same type. Why am I wrong? Joe >> >>int* and long* are two different types (both pointer types). >> >>I'm curious, what led you to think that they're the same type? > > > What's to stop me from swapping an int * and a long *? Joe Assume sizeof(int) < sizeof(long), and consider long l = 42; int *ip = &l; /* illegal, but Let's Pretend */ *ip = 76; What value is now stored in `l'? That's why int* and long* aren't interchangeable. -- Eric Sosman esosman@acm-dot-org.invalid |
Re: pointer q
"Eric Sosman" <esosman@acm-dot-org.invalid> wrote in message news:QpSdnVM7F9qwuvrZRVn-iw@comcast.com... > Joe Smith wrote: > >> "Keith Thompson" <kst-u@mib.org> wrote in message >> news:lnlkt5bqv3.fsf@nuthaus.mib.org... >> >>>"Joe Smith" <grumpy196884@netzero.net> writes: >>> >>>>A recent post of mine showed a sufficiently large gaffe on pointers as >>>>to >>>>need to return to K&R 5.1-6 appendix A8.6.1 . So we have type >>>>specifiers: >>>>int >>>>long >>>>. One dreams himself variable names: qwe, qwr, writes >>>> >>>>int qwe; >>>>long qwr; >>>> >>>>and thinks he knows what types he's declared. Had you asked me about: >>>> >>>>int *qwe; >>>>long *qwr; >>>> >>>>I would have said the pointers declared were--while pointng to different >>>>types--of the same type. Why am I wrong? Joe >>> >>>int* and long* are two different types (both pointer types). >>> >>>I'm curious, what led you to think that they're the same type? >> >> >> What's to stop me from swapping an int * and a long *? Joe > > Assume sizeof(int) < sizeof(long), and consider > > long l = 42; > int *ip = &l; /* illegal, but Let's Pretend */ > *ip = 76; > > What value is now stored in `l'? > > That's why int* and long* aren't interchangeable. Thank you for replies. Mr. Thompson establishes that the reason not to swap an int * and long * is that, as usual, assigning one type to another is illegal, and that you can kludge away with castes if you so desire. Mr Kasak and Mr Sosman show examples of bad things that would happen if you did. What I'm trying to get my head around is how to view the memory with C. If it is true that memory is a bunch of boxes with numbers on the side and that different implementations may have differing sizes of pointers, then the numbers on the sides of those boxes are less homogenous than I thought. Joe |
Re: pointer q
Joe Smith wrote:
<snip> > Thank you for replies. Mr. Thompson establishes that the reason not to swap > an int * and long * is that, as usual, assigning one type to another is > illegal, and that you can kludge away with castes if you so desire. Mr > Kasak and Mr Sosman show examples of bad things that would happen if you > did. What I'm trying to get my head around is how to view the memory with > C. If it is true that memory is a bunch of boxes with numbers on the side > and that different implementations may have differing sizes of pointers, > then the numbers on the sides of those boxes are less homogenous than I > thought. Joe Don't think of memory as a bunch of boxes with numbers on the side. Don't think of memory at all. Think instead of arrays, ints and other objects just floating randomly in space 42 dimensional space (OK, you don't need to use that many dimensions). This will get rid of lots of misconceptions about pointers. For example, when objects can be left, right, above or below each other you start to see why you can't compare two pointer to see which is larger if they don't point in to the same object although you can always compare them for equality. Some of the boxes have names (the ones for variables you declare) and others are created by calling malloc. -- Flash Gordon, living in interesting times. Web site - http://home.flash-gordon.me.uk/ comp.lang.c posting guidelines and intro: http://clc-wiki.net/wiki/Intro_to_clc |
Re: pointer q
Joe Smith wrote:
> "Eric Sosman" <esosman@acm-dot-org.invalid> wrote in message > news:QpSdnVM7F9qwuvrZRVn-iw@comcast.com... > >>Joe Smith wrote: >> >> >>>"Keith Thompson" <kst-u@mib.org> wrote in message >>>news:lnlkt5bqv3.fsf@nuthaus.mib.org... >>> >>> >>>>"Joe Smith" <grumpy196884@netzero.net> writes: >>>> >>>> >>>>>A recent post of mine showed a sufficiently large gaffe on pointers as >>>>>to >>>>>need to return to K&R 5.1-6 appendix A8.6.1 . So we have type >>>>>specifiers: >>>>>int >>>>>long >>>>>. One dreams himself variable names: qwe, qwr, writes >>>>> >>>>>int qwe; >>>>>long qwr; >>>>> >>>>>and thinks he knows what types he's declared. Had you asked me about: >>>>> >>>>>int *qwe; >>>>>long *qwr; >>>>> >>>>>I would have said the pointers declared were--while pointng to different >>>>>types--of the same type. Why am I wrong? Joe >>>> >>>>int* and long* are two different types (both pointer types). >>>> >>>>I'm curious, what led you to think that they're the same type? >>> >>> >>>What's to stop me from swapping an int * and a long *? Joe >> >> Assume sizeof(int) < sizeof(long), and consider >> >>long l = 42; >>int *ip = &l; /* illegal, but Let's Pretend */ >>*ip = 76; >> >>What value is now stored in `l'? >> >> That's why int* and long* aren't interchangeable. > > > Thank you for replies. Mr. Thompson establishes that the reason not to swap > an int * and long * is that, as usual, assigning one type to another is > illegal, and that you can kludge away with castes if you so desire. Mr > Kasak and Mr Sosman show examples of bad things that would happen if you > did. What I'm trying to get my head around is how to view the memory with > C. If it is true that memory is a bunch of boxes with numbers on the side > and that different implementations may have differing sizes of pointers, > then the numbers on the sides of those boxes are less homogenous than I > thought. Joe What you're missing, I think, is that a pointer carries more information than just the "number on the side" of the box it points to. Specifically, it carries information about how many adjacent boxes are involved, and how their contents are to be understood. On many machines, for example, an int* holds the "number" of a box along with the information that three additional boxes are involved and that the four boxes taken together represent an int. A double* might point to the same box number, but could imply that seven more boxes are to be considered along with the first, and that the eight taken as a whole contain a double value. 42 and 42.0 probably don't look the same. The "number on the side of a box" is its address. A pointer denotes not only an address but also a type; a pointer is a "decorated" address. -- Eric Sosman esosman@acm-dot-org.invalid |
| All times are GMT. The time now is 07:15 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.