Why does C need pointer conversion

This is a discussion on Why does C need pointer conversion within the C Programming forums, part of the General Programming Boards category; Hi K&R II says Code: If the type of an expression or subexpression is ``array of T,'' for some type ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    48

    Unhappy Why does C need pointer conversion

    Hi

    K&R II says
    Code:
    If the type of an expression or subexpression is ``array of T,'' for some type T, then the
     value of the expression is a pointer to the first object in the array, and the type of the 
    expression is altered to ``pointer to T.'' This conversion does not take place if the 
    expression is in the operand of the unary & operator, or of ++, --, sizeof, or as the left 
    operand of an assignment operator or the . operator. Similarly, an expression of type 
    ``function returning T,'' except when used as the operand of the & operator, is 
    converted to ``pointer to function returning T.''
    I wonder why C needs to do this conversion for "array of T" and "function returning T". For the array operator [], the required syntax is "pointer to T"[intergral type]. I think the operator [] demands the conversion: "array of T" -> "pointer to T". But in other cases, it seems there is no need to do this conversion, like passing an array to a function. Still, a pointer is passed.

    Furthermore, K&R tells the conversion doesn't take place for some operators. I still try to figure out why. I got some ideas but can't confirm: ++, -- and all assignment operators all generate "side effects" while array is not a modifiable lvalue. The dot operator requires a structure or union as its first operand. So it's "array of T" which doesn't fit in the operand requirement, and it has nothing to do with pointer conversion. & and sizeof require "pointer to an array" and "size of an array " respectively, not "pointer to a pointer" and "size of a pointer". So it's a semantic issue here. My point is pointer conversion seems a default action in a C program, with some exceptions. When "array of T" can participate in some operation, it will participate in the type "pointer to T". I need a direction to understand this mechanism. Can anyone help?

    Thanks in advance!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    Quote Originally Posted by password636
    I wonder why C needs to do this conversion for "array of T" and "function returning T". For the array operator [], the required syntax is "pointer to T"[intergral type]. I think the operator [] demands the conversion: "array of T" -> "pointer to T". But in other cases, it seems there is no need to do this conversion, like passing an array to a function. Still, a pointer is passed.
    I would say that one reason is efficiency: it is typically undesirable to copy an array since it may hold many objects which in turn are possibly expensive to copy. For functions, copying a function usually does not make much sense (unless the function itself can hold state, as is the case for function objects in say, C++).

    Quote Originally Posted by password636
    My point is pointer conversion seems a default action in a C program, with some exceptions. When "array of T" can participate in some operation, it will participate in the type "pointer to T". I need a direction to understand this mechanism. Can anyone help?
    I think you already have the right idea in saying that an "array is not a modifiable lvalue". In the case of operator &, consider that the type of the result would be "pointer to the array" rather than "pointer to pointer to element of the array", hence the conversion should not happen. Likewise for a function, since this syntax already is suitable for getting a pointer to the function. In the case of sizeof, consider that it does not evaluate its operand, hence if the operand really is an array, it is easy to think that it would return the total size of the array, but if it were a pointer, it would return the size of the pointer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Quote Originally Posted by laserlight View Post
    I would say that one reason is efficiency: it is typically undesirable to copy an array since it may hold many objects which in turn are possibly expensive to copy. For functions, copying a function usually does not make much sense (unless the function itself can hold state, as is the case for function objects in say, C++).
    Yes. This inspires me. When an expression of "array of T" or "function returning T" can participate in some operation and the operation uses the VALUE of the expression, it will be altered to "pointer to T" or "pointer to function". The operation uses the pointer like some kind of entry to the array or function and get their VALUE. This can save storage space and other resources. In a word, pointer is a more effective and sufficient way when retrieving the VALUE of the expression is only needed. And if the operation uses its other information instead of the VALUE, such as & and sizeof operators, it will participate as the way it actually is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  2. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 10:31 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. help me in conversion using pointer
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 08-20-2002, 12:15 AM
  5. Replies: 2
    Last Post: 02-07-2002, 08:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21