Thread: Implicit conversion between pointers?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    27

    Implicit conversion between pointers?

    I am reading K&R book. In section 5.3 there is this code:

    Code:
        int a[10];
        int *pa;
        pa=a;
    I think that should be:
    Code:
        int a[10];
        int *pa;
        pa=(int *)a;
    Because pa is a pointer to an integer and a is a pointer to an array of integers. Of course the first code works maybe because it happens an implicit cast (conversion). Am I right?

    thanks

  2. #2
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    A pointer to an array of integer is an int *, so there is no need for conversion, since pa and a are of the same type.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    27
    Thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nonlinearly
    Because pa is a pointer to an integer and a is a pointer to an array of integers.
    pa is a pointer to an int. a is an array, not a pointer to an array of integers. This declares a pointer to an array of 10 int:
    Code:
    int (*p)[10];
    Quote Originally Posted by nonlinearly
    Of course the first code works maybe because it happens an implicit cast (conversion). Am I right?
    Yes, a is converted to a pointer to its first element, i.e., a pointer to an int. The cast is unnecessary.

    Quote Originally Posted by Tibo-88
    A pointer to an array of integer is an int *, so there is no need for conversion, since pa and a are of the same type.
    No, that is not true. pa and a are not of the same type. It is easy to demonstrate by running this program:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int a[10];
        int *pa;
        printf("%u %u\n", sizeof(a), sizeof(pa));
        return 0;
    }
    if a and pa were of the same type, then sizeof(a) == sizeof(pa) must be true.
    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

  5. #5
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    Sorry for my mistake, I guess you're right! But what is the difference of type then? They're both int* and if you do pa=a, you can access pa[x] as well as if you did a[x], so pa sort of becomes an array too. Could you explain me that please?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Tibo-88
    But what is the difference of type then? They're both int* and if you do pa=a, you can access pa[x] as well as if you did a[x], so pa sort of becomes an array too. Could you explain me that please?
    As I mentioned, a is converted to a pointer to its first element. Concerning the notation, pa[x] is equivalent to *(pa + x).
    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

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    27
    I came back...
    I think that the original question should be with 2 dimensional array like:
    Code:
    int array[2][3];
    int (*p2a)[3];
    p2a=(int (*)[3])array;
    In this situation the cast is necessary...!!!

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, the cast is still unnecessary.
    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

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    27
    Sorry my fault.. I will correct it

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Huh? What are you trying to do now? Your code snippet is wrong because a pointer to an array of 3 ints is not convertible to a pointer to an int.
    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

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    27
    SORRY... I meant
    Code:
    int array[2][3];
    int *ptr;
    ptr=(int *)array;

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah, then yes, there is no implicit conversion.
    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

  13. #13
    Registered User
    Join Date
    Nov 2011
    Posts
    27
    The difference is that with this approach we can not refer to an element using subscript notation (for example ptr[1][2] is not valid).
    thanks for your responses... you have helped me to understand very well...

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nonlinearly
    The difference is that with this approach we can not refer to an element using subscript notation (for example ptr[1][2] is not valid).
    It isn't really a difference in approach: it is the difference between using a pointer to an array of int and using a pointer to an int.

    Quote Originally Posted by nonlinearly
    thanks for your responses... you have helped me to understand very well...
    You're welcome
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implicit conversion problem
    By Elysia in forum C++ Programming
    Replies: 3
    Last Post: 03-02-2008, 01:38 PM
  2. implicit/explicit? conversion of classes?
    By what3v3r in forum C++ Programming
    Replies: 7
    Last Post: 01-31-2006, 07:08 PM
  3. implicit conversion not working
    By Mr_Jack in forum C++ Programming
    Replies: 4
    Last Post: 03-09-2004, 10:50 AM
  4. implicit conversion
    By cj56 in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2003, 11:36 AM
  5. Error: Implicit conversion from void*
    By Unregistered in forum Windows Programming
    Replies: 5
    Last Post: 12-12-2001, 02:38 PM