length of array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • prog00@googlemail.com

    #1

    length of array

    Hi,
    I write the following code:

    void someproc() {
    int controlids [] = {
    IDC_BUTTON_RC_C ONNECT,
    IDC_BUTTON_RC_C ONNECTPUBLIC,
    IDC_BTN_SENDHEL LO };

    someproc2(contr olids);
    }

    void someproc2(int * e)
    {
    int size = sizeof(e)/sizeof(int);
    //.. do something with size.. like use in a for loop etc...

    }

    I expected to get the correct size, but it just gives me 1. What am I
    doing wrong?

    regards,

    X.

  • chenchen

    #2
    Re: length of array

    because void someproc2(int * e) is wrong
    in the scope e is changed to point type !
    so int size = sizeof(e)/sizeof(int); size will == 1

    Comment

    • prog00@googlemail.com

      #3
      Re: length of array

      So what should the signature of someproc2 be to make things right?

      Comment

      • Python.LeoJay@gmail.com

        #4
        Re: length of array

        because array formal argument decays to a pointer to its first element
        and the array bound is ignored.
        so, IMHO, the only way to achieve your purpose is
        void someproc() {
        int controlids [] = {
        IDC_BUTTON_RC_C ONNECT,
        IDC_BUTTON_RC_C ONNECTPUBLIC,
        IDC_BTN_SENDHEL LO };


        someproc2(contr olids, sizeof(controli ds)/sizeof(controli ds[0]));



        }


        void someproc2(int * e, int size)
        {
        //.. do something with size.. like use in a for loop etc...


        }

        Comment

        • prog00@googlemail.com

          #5
          Re: length of array

          Alright, thanks.

          X.

          Comment

          • Kai-Uwe Bux

            #6
            Re: length of array

            prog00@googlema il.com wrote:
            [color=blue]
            > Hi,
            > I write the following code:
            >
            > void someproc() {
            > int controlids [] = {
            > IDC_BUTTON_RC_C ONNECT,
            > IDC_BUTTON_RC_C ONNECTPUBLIC,
            > IDC_BTN_SENDHEL LO };
            >
            > someproc2(contr olids);
            > }
            >
            > void someproc2(int * e)
            > {
            > int size = sizeof(e)/sizeof(int);
            > //.. do something with size.. like use in a for loop etc...
            >
            > }
            >
            > I expected to get the correct size, but it just gives me 1. What am I
            > doing wrong?[/color]

            Your expectations were met: you got the correct size.

            Note the signature: void someproc2(int * e ). You say that e is a variable
            of type int*. Well, the size of a pointer to int on your machine just
            happens to be the same as the size of an int. Note that the size of an int*
            says nothing about the length of the array to whose first element that
            pointer is pointing.


            Best

            Kai-Uwe Bux

            ps.: If you want to keep track of the length of an array, consider using
            std::vector.

            Comment

            • Tomás

              #7
              Re: length of array

              [color=blue]
              > void someproc() {
              > int controlids [] = {
              > IDC_BUTTON_RC_C ONNECT,
              > IDC_BUTTON_RC_C ONNECTPUBLIC,
              > IDC_BTN_SENDHEL LO };
              >
              > someproc2(contr olids);
              > }
              >
              > void someproc2(int * e)
              > {
              > int size = sizeof(e)/sizeof(int);
              > //.. do something with size.. like use in a for loop etc...
              >
              > }[/color]


              Could use a template:


              void someproc()
              {
              int controlids [] = {
              IDC_BUTTON_RC_C ONNECT,
              IDC_BUTTON_RC_C ONNECTPUBLIC,
              IDC_BTN_SENDHEL LO };

              someproc2(contr olids);
              }

              template<class T, unsigned i>
              void someproc2(int (&array)[i])
              {
              int size = i;
              }


              -Tomás

              Comment

              • Default User

                #8
                Re: length of array

                chenchen wrote:
                [color=blue]
                > because void someproc2(int * e) is wrong
                > in the scope e is changed to point type !
                > so int size = sizeof(e)/sizeof(int); size will == 1[/color]

                Please read the information below.


                Brian

                --
                Please quote enough of the previous message for context. To do so from
                Google, click "show options" and use the Reply shown in the expanded
                header.

                Comment

                • Default User

                  #9
                  Re: length of array

                  prog00@googlema il.com wrote:
                  [color=blue]
                  > So what should the signature of someproc2 be to make things right?[/color]

                  Please read the information below.


                  Brian

                  --
                  Please quote enough of the previous message for context. To do so from
                  Google, click "show options" and use the Reply shown in the expanded
                  header.

                  Comment

                  • Default User

                    #10
                    Re: length of array

                    Python.LeoJay@g mail.com wrote:
                    [color=blue]
                    > because array formal argument decays to a pointer to its first element
                    > and the array bound is ignored.[/color]

                    Please read the information below.


                    Brian

                    --
                    Please quote enough of the previous message for context. To do so from
                    Google, click "show options" and use the Reply shown in the expanded
                    header.

                    Comment

                    Working...