Differentiate between Array and Pointer in function parameter - C++












0















This is a problem I've been struggling with for a while now.
The codes I have tried below are not compiling either.



The question is: How do I differentiate between a pointer and a fixed array in a function parameter?



// Concepts for the Array I have tried but not succeeded.
template <size_t length, typename type>
const unsigned int len(type arg[static length]) { return length; }

template <size_t length, typename type>
const unsigned int len(type(&)[length]) { return length; }

// This works for Arrays & Pointers
// but should be less prioritized over the function that detects the Array
template <typename type> const unsigned int len(type* arg) { return sizeof(*arg); }


I know how arrays & pointers are basically similar when passed into a function, but it asks the question: is there not a way to tell them apart?



Syntactically yes, but otherwise what other way is there?



Anyway, thanks for reading through and cheers on your response.










share|improve this question


















  • 2





    static length is not valid C++. That particular C99 feature is not available.

    – StoryTeller
    Nov 21 '18 at 6:30


















0















This is a problem I've been struggling with for a while now.
The codes I have tried below are not compiling either.



The question is: How do I differentiate between a pointer and a fixed array in a function parameter?



// Concepts for the Array I have tried but not succeeded.
template <size_t length, typename type>
const unsigned int len(type arg[static length]) { return length; }

template <size_t length, typename type>
const unsigned int len(type(&)[length]) { return length; }

// This works for Arrays & Pointers
// but should be less prioritized over the function that detects the Array
template <typename type> const unsigned int len(type* arg) { return sizeof(*arg); }


I know how arrays & pointers are basically similar when passed into a function, but it asks the question: is there not a way to tell them apart?



Syntactically yes, but otherwise what other way is there?



Anyway, thanks for reading through and cheers on your response.










share|improve this question


















  • 2





    static length is not valid C++. That particular C99 feature is not available.

    – StoryTeller
    Nov 21 '18 at 6:30
















0












0








0








This is a problem I've been struggling with for a while now.
The codes I have tried below are not compiling either.



The question is: How do I differentiate between a pointer and a fixed array in a function parameter?



// Concepts for the Array I have tried but not succeeded.
template <size_t length, typename type>
const unsigned int len(type arg[static length]) { return length; }

template <size_t length, typename type>
const unsigned int len(type(&)[length]) { return length; }

// This works for Arrays & Pointers
// but should be less prioritized over the function that detects the Array
template <typename type> const unsigned int len(type* arg) { return sizeof(*arg); }


I know how arrays & pointers are basically similar when passed into a function, but it asks the question: is there not a way to tell them apart?



Syntactically yes, but otherwise what other way is there?



Anyway, thanks for reading through and cheers on your response.










share|improve this question














This is a problem I've been struggling with for a while now.
The codes I have tried below are not compiling either.



The question is: How do I differentiate between a pointer and a fixed array in a function parameter?



// Concepts for the Array I have tried but not succeeded.
template <size_t length, typename type>
const unsigned int len(type arg[static length]) { return length; }

template <size_t length, typename type>
const unsigned int len(type(&)[length]) { return length; }

// This works for Arrays & Pointers
// but should be less prioritized over the function that detects the Array
template <typename type> const unsigned int len(type* arg) { return sizeof(*arg); }


I know how arrays & pointers are basically similar when passed into a function, but it asks the question: is there not a way to tell them apart?



Syntactically yes, but otherwise what other way is there?



Anyway, thanks for reading through and cheers on your response.







c++






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 6:24









LapysLapys

3961313




3961313








  • 2





    static length is not valid C++. That particular C99 feature is not available.

    – StoryTeller
    Nov 21 '18 at 6:30
















  • 2





    static length is not valid C++. That particular C99 feature is not available.

    – StoryTeller
    Nov 21 '18 at 6:30










2




2





static length is not valid C++. That particular C99 feature is not available.

– StoryTeller
Nov 21 '18 at 6:30







static length is not valid C++. That particular C99 feature is not available.

– StoryTeller
Nov 21 '18 at 6:30














2 Answers
2






active

oldest

votes


















1














This approach works for me:



#include <stdio.h>

template<typename T, int size> unsigned int len(const T(&)[size]) {printf("The number of items in your array is: %in", size); return size;}
template<typename T> unsigned int len(const T * p) {printf("The size of the item your pointer points to is: %zun", sizeof(*p)); return sizeof(*p);}

int main(int, char **)
{
int myArray[10];
int * myPointer = myArray;

(void) len(myArray);
(void) len(myPointer);

return 0;
}


... when I run it, it prints out:



The number of items in your array is:  10
The size of the item your pointer points to is: 4





share|improve this answer































    1














    The length of an array can only be deduced if the array is passed by reference, otherwise it decays to pointer:



    template<typename T, std::size_t length>
    constexpr std::size_t len(const T(&)[length]) { return length; }
    // ^^^

    template<typename T>
    constexpr std::size_t len(const T *&p) { return sizeof *p; }
    // ^^^




    Full demo:



    #include <cstdlib>

    template<typename T, std::size_t length>
    constexpr std::size_t len(const T(&)[length]) { return length; }

    template<typename T>
    constexpr std::size_t len(const T *&p) { return sizeof *p; }

    #include <iostream>
    int main(int, char **)
    {
    const int array[7] = {};
    const int *pointer = array;

    std::cout << "array has " << len(array) << " itemsn"
    << "and pointer is to " << len(pointer) << " charsn";
    }





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406319%2fdifferentiate-between-array-and-pointer-in-function-parameter-c%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      This approach works for me:



      #include <stdio.h>

      template<typename T, int size> unsigned int len(const T(&)[size]) {printf("The number of items in your array is: %in", size); return size;}
      template<typename T> unsigned int len(const T * p) {printf("The size of the item your pointer points to is: %zun", sizeof(*p)); return sizeof(*p);}

      int main(int, char **)
      {
      int myArray[10];
      int * myPointer = myArray;

      (void) len(myArray);
      (void) len(myPointer);

      return 0;
      }


      ... when I run it, it prints out:



      The number of items in your array is:  10
      The size of the item your pointer points to is: 4





      share|improve this answer




























        1














        This approach works for me:



        #include <stdio.h>

        template<typename T, int size> unsigned int len(const T(&)[size]) {printf("The number of items in your array is: %in", size); return size;}
        template<typename T> unsigned int len(const T * p) {printf("The size of the item your pointer points to is: %zun", sizeof(*p)); return sizeof(*p);}

        int main(int, char **)
        {
        int myArray[10];
        int * myPointer = myArray;

        (void) len(myArray);
        (void) len(myPointer);

        return 0;
        }


        ... when I run it, it prints out:



        The number of items in your array is:  10
        The size of the item your pointer points to is: 4





        share|improve this answer


























          1












          1








          1







          This approach works for me:



          #include <stdio.h>

          template<typename T, int size> unsigned int len(const T(&)[size]) {printf("The number of items in your array is: %in", size); return size;}
          template<typename T> unsigned int len(const T * p) {printf("The size of the item your pointer points to is: %zun", sizeof(*p)); return sizeof(*p);}

          int main(int, char **)
          {
          int myArray[10];
          int * myPointer = myArray;

          (void) len(myArray);
          (void) len(myPointer);

          return 0;
          }


          ... when I run it, it prints out:



          The number of items in your array is:  10
          The size of the item your pointer points to is: 4





          share|improve this answer













          This approach works for me:



          #include <stdio.h>

          template<typename T, int size> unsigned int len(const T(&)[size]) {printf("The number of items in your array is: %in", size); return size;}
          template<typename T> unsigned int len(const T * p) {printf("The size of the item your pointer points to is: %zun", sizeof(*p)); return sizeof(*p);}

          int main(int, char **)
          {
          int myArray[10];
          int * myPointer = myArray;

          (void) len(myArray);
          (void) len(myPointer);

          return 0;
          }


          ... when I run it, it prints out:



          The number of items in your array is:  10
          The size of the item your pointer points to is: 4






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 6:30









          Jeremy FriesnerJeremy Friesner

          39.3k1080161




          39.3k1080161

























              1














              The length of an array can only be deduced if the array is passed by reference, otherwise it decays to pointer:



              template<typename T, std::size_t length>
              constexpr std::size_t len(const T(&)[length]) { return length; }
              // ^^^

              template<typename T>
              constexpr std::size_t len(const T *&p) { return sizeof *p; }
              // ^^^




              Full demo:



              #include <cstdlib>

              template<typename T, std::size_t length>
              constexpr std::size_t len(const T(&)[length]) { return length; }

              template<typename T>
              constexpr std::size_t len(const T *&p) { return sizeof *p; }

              #include <iostream>
              int main(int, char **)
              {
              const int array[7] = {};
              const int *pointer = array;

              std::cout << "array has " << len(array) << " itemsn"
              << "and pointer is to " << len(pointer) << " charsn";
              }





              share|improve this answer




























                1














                The length of an array can only be deduced if the array is passed by reference, otherwise it decays to pointer:



                template<typename T, std::size_t length>
                constexpr std::size_t len(const T(&)[length]) { return length; }
                // ^^^

                template<typename T>
                constexpr std::size_t len(const T *&p) { return sizeof *p; }
                // ^^^




                Full demo:



                #include <cstdlib>

                template<typename T, std::size_t length>
                constexpr std::size_t len(const T(&)[length]) { return length; }

                template<typename T>
                constexpr std::size_t len(const T *&p) { return sizeof *p; }

                #include <iostream>
                int main(int, char **)
                {
                const int array[7] = {};
                const int *pointer = array;

                std::cout << "array has " << len(array) << " itemsn"
                << "and pointer is to " << len(pointer) << " charsn";
                }





                share|improve this answer


























                  1












                  1








                  1







                  The length of an array can only be deduced if the array is passed by reference, otherwise it decays to pointer:



                  template<typename T, std::size_t length>
                  constexpr std::size_t len(const T(&)[length]) { return length; }
                  // ^^^

                  template<typename T>
                  constexpr std::size_t len(const T *&p) { return sizeof *p; }
                  // ^^^




                  Full demo:



                  #include <cstdlib>

                  template<typename T, std::size_t length>
                  constexpr std::size_t len(const T(&)[length]) { return length; }

                  template<typename T>
                  constexpr std::size_t len(const T *&p) { return sizeof *p; }

                  #include <iostream>
                  int main(int, char **)
                  {
                  const int array[7] = {};
                  const int *pointer = array;

                  std::cout << "array has " << len(array) << " itemsn"
                  << "and pointer is to " << len(pointer) << " charsn";
                  }





                  share|improve this answer













                  The length of an array can only be deduced if the array is passed by reference, otherwise it decays to pointer:



                  template<typename T, std::size_t length>
                  constexpr std::size_t len(const T(&)[length]) { return length; }
                  // ^^^

                  template<typename T>
                  constexpr std::size_t len(const T *&p) { return sizeof *p; }
                  // ^^^




                  Full demo:



                  #include <cstdlib>

                  template<typename T, std::size_t length>
                  constexpr std::size_t len(const T(&)[length]) { return length; }

                  template<typename T>
                  constexpr std::size_t len(const T *&p) { return sizeof *p; }

                  #include <iostream>
                  int main(int, char **)
                  {
                  const int array[7] = {};
                  const int *pointer = array;

                  std::cout << "array has " << len(array) << " itemsn"
                  << "and pointer is to " << len(pointer) << " charsn";
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 9:43









                  Toby SpeightToby Speight

                  16.7k134165




                  16.7k134165






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406319%2fdifferentiate-between-array-and-pointer-in-function-parameter-c%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      Npm cannot find a required file even through it is in the searched directory