Re: pointer q




"Eric Sosman" <esosman@xxxxxxxxxxxxxxxxxxx> wrote in message
news:QpSdnVM7F9qwuvrZRVn-iw@xxxxxxxxxxxxxx
Joe Smith wrote:

"Keith Thompson" <kst-u@xxxxxxx> wrote in message
news:lnlkt5bqv3.fsf@xxxxxxxxxxxxxxxxxx

"Joe Smith" <grumpy196884@xxxxxxxxxxx> 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


.



Relevant Pages

  • pointer q
    ... A recent post of mine showed a sufficiently large gaffe on pointers as to ... One dreams himself variable names: qwe, qwr, writes ... int qwe; ...
    (comp.lang.c)
  • Re: pointer q
    ... One dreams himself variable names: qwe, qwr, writes ... int qwe; ...
    (comp.lang.c)
  • Re: pointer q
    ... qwe, qwr, writes ... int qwe; ... Joe ...
    (comp.lang.c)
  • Re: pointer q
    ... qwe, qwr, writes ... int qwe; ... You can do the assignments with explicit conversion, ...
    (comp.lang.c)
  • Re: pointer q
    ... Joe Smith wrote: ... qwe, qwr, writes ... int qwe; ...
    (comp.lang.c)