Thread: Returning Array

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The whole passing array is silly. You're just passing a pointer plain and simple. Always. There's no passing array, just passing a pointer. So if you think like that, there's no decaying in the first place.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The whole passing array is silly. You're just passing a pointer plain and simple. Always. There's no passing array, just passing a pointer. So if you think like that, there's no decaying in the first place.
    I am not sure about C, but the C++ Standard states quite clearly that an array to pointer conversion is performed. If you are not passing an array, such a conversion does not make sense. As such, I conclude that one is indeed passing an array, but this array is converted to a pointer to its first element.
    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. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't know why they're making such a fuss about it. Why be able to pass an array when you're just passing a pointer?
    Anyway, that's how I see it, in my humble opinion.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why be able to pass an array when you're just passing a pointer?
    Simply because you are not passing a pointer, unless what you pass really is a pointer to an element in the array. Consider:
    Code:
    #include <stddef.h>
    
    void foo(int *y, size_t size);
    
    int main(void)
    {
        int x[10];
        foo(x, 10);
        return 0;
    }
    
    void foo(int *y, size_t size)
    {
        // ...
    }
    Within main(), x is an array of 10 ints. Within foo(), y is a pointer to an element (the first, in this case) of an array of size (10, in this case) ints. But we call foo(x, 10), hence we are passing x, which is an array, not a pointer. Thus, array to pointer conversion is performed (i.e., the array decays to a pointer to its first element).

    To claim that we are just passing a pointer is to insist that x is a pointer when it really is an array.
    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. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I could as well argue that an array is just a pointer. A pointer to a contiguous memory space with allocated space for 10 ints.
    We can just as well do x[5] on a pointer as well as array, which would mean to me that an array is just a pointer. There's no bounds checking on either.
    Therefore, an array is a pointer, so you are passing a pointer, not an array.
    That's my take on the whole situation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I could as well argue that an array is just a pointer.
    So you claim that sizeof on an array and sizeof on an equivalent pointer will always be the same?
    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. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, but when it's an array, the compiler knows the size, because you defined it locally, thus the compiler can look back and find the size.
    It's not really possible to pass an array in my opinion. If we could, then the compiler should be able to know the size of the array at all times.
    It's similar, because when you do sizeof("My string"), it won't return the same as sizeof(cosnt char*), but "My string" is basically a const char*.
    The standard could say what it wants, it's in no way wrong, but this is my belief on the whole.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, but when it's an array, the compiler knows the size, because you defined it locally, thus the compiler can look back and find the size.
    As such, arrays and pointers are not the same. An array is not just a pointer. However...

    It's not really possible to pass an array in my opinion. If we could, then the compiler should be able to know the size of the array at all times.
    I think you're looking at things under the hood, so to speak. If we look from the view point of C and C++ an array is not a pointer, even though that is what it really is when it comes down to it. So passing an array actually involves passing a pointer, but because (in this case) what we pass is not of a pointer type, we say that we pass an array and there is an array to pointer conversion.

    My main objection to stating that "There's no passing array, just passing a pointer." is that it is misleading: if the caller passes a pointer, the caller has a pointer, so arrays are pointers. Yet from your own observation "when it's an array, the compiler knows the size, because you defined it locally", so there is the implication that arrays are not pointers. This would then be a contradiction, unless we talk in terms of array to pointer conversion.

    It's similar, because when you do sizeof("My string"), it won't return the same as sizeof(cosnt char*), but "My string" is basically a const char*.
    No, "My string" is a const char[10], not a const char*.
    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. #24
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    When I think of arrays, I think of pointers. There may be some slight technical difference under the hood, but from my point of view, they act the same (except sizeof) so they are the same.

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    I think you're looking at things under the hood, so to speak. If we look from the view point of C and C++ an array is not a pointer, even though that is what it really is when it comes down to it. So passing an array actually involves passing a pointer, but because (in this case) what we pass is not of a pointer type, we say that we pass an array and there is an array to pointer conversion.

    My main objection to stating that "There's no passing array, just passing a pointer." is that it is misleading: if the caller passes a pointer, the caller has a pointer, so arrays are pointers. Yet from your own observation "when it's an array, the compiler knows the size, because you defined it locally", so there is the implication that arrays are not pointers. This would then be a contradiction, unless we talk in terms of array to pointer conversion.
    So let's put it a little more clearly, perhaps. The array is a pointer, since you can easily do
    Code:
    char str[10];
    char* pStr = str;
    And if you would do sizeof(pStr), you would just get 4 (on x86), of course, because the compiler looks at the name as sees it as a pointer.
    For sizeof(str), the compiler sees a pointer with which you have told it to allocate a number of elements, thus it can return the correct size.

    I see it as [] is a special syntax for declaring a pointer to a memory space on the stack which holds space for n elements.

    No, "My string" is a const char[10], not a const char*.
    Well, perhaps in language terms it is, but a string (C-style) is just a char* to me. But since you have specified how many characters are in the string, the compiler can deduce the size.

    By not having an "array," just pointers, it's easy not to confuse arrays and pointers and how an array would decay to a pointer.
    It's how I see it anyway.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By not having an "array," just pointers, it's easy not to confuse arrays and pointers and how an array would decay to a pointer.
    It's how I see it anyway.
    I can see where you are coming from, i.e., you are saying that you find it simpler to just think of the array as a pointer, despite the (minor) language imposed differences.

    Frankly, I do not really care either way, since these are just different ways of looking at the same thing. I do care that you were contradicting me to the point of calling my statement "silly" without stating that this was just how you viewed it, but now that it is out in the open I have nothing to add
    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

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    Frankly, I do not really care either way, since these are just different ways of looking at the same thing. I do care that you were contradicting me to the point of calling my statement "silly" without stating that this was just how you viewed it, but now that it is out in the open I have nothing to add
    Ah, right, so when calling something silly without backing it up with why is a bad idea?
    I also have a loop that uses goto to break out of it actually, with no breaks at all and lots of continue. It's a larger big loop. Perhaps it could be broken into smaller parts someday.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing and returning array pointers?
    By thealmightyone in forum C Programming
    Replies: 26
    Last Post: 03-26-2009, 03:38 PM
  2. Returning an Array
    By mmarab in forum C Programming
    Replies: 10
    Last Post: 07-19-2007, 07:05 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM