Re: pointers as parameters



CBFalconer <cbfalconer@xxxxxxxxx> writes:
Mark McIntyre wrote:
mdh wrote:
Ok...will let me then ask this. If there is no difference in the
"actual" pointers of different types that are passed to the
function,

Note that there _may_ in fact be a difference. Pointer
representations need not all be indistinguishable.

then is is correct to say that the reason one declares the
pointer's type as, a "pointer to char" or "pointer to pointer
to char" or "ptr to void" etc, is that during the compilation
of the program, the compiler is able to check the code and
prevent errors at run time?

Yes.

In addition, since the compiler knows the type, it can correctly
interpret such values as "sizeof *ptr" and also detect type
conflicts in such things as "*ptr1 = *ptr2".

And there's nothing special about pointer types in this regard; the
same thing happens with other types as well. For example, suppose int
and float are both 32 bits. There's no real distinction on the
machine level between an int object and a float object; they might
both be stored in exactly the same way. But the compiler knows the
type of each object, so that when you write "x + y" in your program,
it generates an add-integer or an add-float instruction as
appropriate. If you're writing in assembly language, you can just as
easily apply integer or floating-point operations to any chunk of
memory of the right size; in the machine code generated by a compiler,
you won't see floating-point operations on integer objects unless you
do some really ugly pointer-casting.

--
Keith Thompson (The_Other_Keith) <kst-u@xxxxxxx>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: How to convert Infix notation to postfix notation
    ... and make all strings const save where the intent ... function whose contract is to change the string. ... the compiler "just" prevents the string ... try to do using the pointer you get. ...
    (comp.lang.c)
  • Re: Mex Overflow Error Using Free Borland Compiler
    ... >>>would clean up a lot of code by eliminating the pointer dereferences. ... >>>guaranteed by the standard and should not be relied on. ... >>>You can try the GNU C compiler available on mingw or cygwin. ... >>>the Constraints are then both operands shall have arithmetic type. ...
    (comp.soft-sys.matlab)
  • Re: Anybody here endure C/Cpp? (.h to .inc conversion)
    ... Pascal or Stdcall convention... ... "PFNGLPOINTPARAMETERFEXTPROC" to be a type that's a pointer to a function ... DWORD, using a 32-bit compiler, with an address in it...adding the ... this is working on the premise that OpenGL does it like most others ...
    (alt.lang.asm)
  • Re: Why the compiler applies sign extension to unsigned data?
    ... I want the value of p to be made unsigned *before* the cast. ... want the cast to generate a sign extension. ... ULONG_PTR is ULONG and casting the pointer to ULONG_PTR first is the ... on the compiler and on the knowledge of the size of the ptr variable, ...
    (microsoft.public.development.device.drivers)
  • Re: OO programming - illumination? - whoopsie
    ... > represented by a host of loosely related arrays. ... > compiler and the memory allocators. ... A pointer is usually a word. ... organs can be further modeled to include cells and so on and so forth. ...
    (comp.lang.java.programmer)