Thread: Why does C need pointer conversion

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 11: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, 09:39 AM