Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to calculate an array's length?

how to calculate an array's length?

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structurestutorial
10 Posts 7 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Peter Chan 0
    wrote on last edited by
    #1

    A simple question. I define an array.How to calculate its length? int[] arInt = { 1, 2, 3 ,4, 5 }; Thanks in advance.

    K _ B 3 Replies Last reply
    0
    • P Peter Chan 0

      A simple question. I define an array.How to calculate its length? int[] arInt = { 1, 2, 3 ,4, 5 }; Thanks in advance.

      K Offline
      K Offline
      khan
      wrote on last edited by
      #2

      Peter, Chan wrote:

      int[] arInt = { 1, 2, 3 ,4, 5 };

      The correct syntax should be: int arInt[] = {.... int nSize = sizeof(arInt) / sizeof(int);

      this is this.

      P 1 Reply Last reply
      0
      • P Peter Chan 0

        A simple question. I define an array.How to calculate its length? int[] arInt = { 1, 2, 3 ,4, 5 }; Thanks in advance.

        _ Offline
        _ Offline
        _AnsHUMAN_
        wrote on last edited by
        #3

        Peter, Chan wrote:

        I define an array.How to calculate its length

        Did you have a look at the sizeof()

        Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

        1 Reply Last reply
        0
        • K khan

          Peter, Chan wrote:

          int[] arInt = { 1, 2, 3 ,4, 5 };

          The correct syntax should be: int arInt[] = {.... int nSize = sizeof(arInt) / sizeof(int);

          this is this.

          P Offline
          P Offline
          Peter Chan 0
          wrote on last edited by
          #4

          Then if it is a array of string.Should I write like this. char * arLang[] = {"english", "chinese"}; int nSize = sizeof(arLang) / sizeof(char);

          B K C 3 Replies Last reply
          0
          • P Peter Chan 0

            Then if it is a array of string.Should I write like this. char * arLang[] = {"english", "chinese"}; int nSize = sizeof(arLang) / sizeof(char);

            B Offline
            B Offline
            baerten
            wrote on last edited by
            #5

            Nope Then you get the number of char's in the array, not the number of strings You can't count it this way, because each string has a different lenght. You can determine the length of arLang, then loop on it and count the occurence of \0 (End of a String) So you get the number of strings in the array, if i tell no mistakes :-) Good luck

            1 Reply Last reply
            0
            • P Peter Chan 0

              Then if it is a array of string.Should I write like this. char * arLang[] = {"english", "chinese"}; int nSize = sizeof(arLang) / sizeof(char);

              K Offline
              K Offline
              khan
              wrote on last edited by
              #6

              No, it can't be done with multi-dimensional arrays. You can only do it with single-dimensional arrays like: char arLang[] = {"english"}; And the length will be 8 bytes. And whenever you get the operator * anywhere, you just can't be sure of the size. But you can write: int nSize1 = strlen(arLang[0]) + 1; int nSize2 = strlen(arLang[1]) + 1; etc.

              this is this.

              1 Reply Last reply
              0
              • P Peter Chan 0

                Then if it is a array of string.Should I write like this. char * arLang[] = {"english", "chinese"}; int nSize = sizeof(arLang) / sizeof(char);

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                Peter, Chan wrote:

                int nSize = sizeof(arLang) / sizeof(char);

                Mistake! change as follows:

                int nSize = sizeof(arLang) / sizeof(char *);

                or (better):

                int nSize = sizeof(arLang) / sizeof(arLang[0]);

                :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                1 Reply Last reply
                0
                • P Peter Chan 0

                  A simple question. I define an array.How to calculate its length? int[] arInt = { 1, 2, 3 ,4, 5 }; Thanks in advance.

                  B Offline
                  B Offline
                  Bram van Kampen
                  wrote on last edited by
                  #8

                  It depends on what you want. If the array is dynamically declared, i.e. You used 'malloc', there is no knowing, unless if you build in your own scheme of determining these things. At any rate, the size of the array is known to you, because you had to give the size to the malloc function. In these cases, you need to devine a scheme to pass this knowledge on to where it is needed. in statically declared arrays, you can use the sizeof() operator to get the size of an array in bytes. But, still again, beware and look at the language definition. i.e.: char * Arr[]={"First Item","Second Item"}; This is an array of pointers to character strings. Arr contains two 4-Bit Items ( or 2 8-Bit items in a 64 bit Compiler), namely the Addresses of the two strings, "First Item" and "Second Item". So in the above example, sizeof(ARR) would always give 8 ( or 16 in a 64 bit Compiler environment). If you need to know wat's needed to store the structure ans it's data, you need:- size_t GetTotalDataSize(){ size_t NrOfItems= sizeof(Arr)/sizeof(char*); size_t TotalSize=sizeof(Arr); for(int i=0;i

                  P 1 Reply Last reply
                  0
                  • B Bram van Kampen

                    It depends on what you want. If the array is dynamically declared, i.e. You used 'malloc', there is no knowing, unless if you build in your own scheme of determining these things. At any rate, the size of the array is known to you, because you had to give the size to the malloc function. In these cases, you need to devine a scheme to pass this knowledge on to where it is needed. in statically declared arrays, you can use the sizeof() operator to get the size of an array in bytes. But, still again, beware and look at the language definition. i.e.: char * Arr[]={"First Item","Second Item"}; This is an array of pointers to character strings. Arr contains two 4-Bit Items ( or 2 8-Bit items in a 64 bit Compiler), namely the Addresses of the two strings, "First Item" and "Second Item". So in the above example, sizeof(ARR) would always give 8 ( or 16 in a 64 bit Compiler environment). If you need to know wat's needed to store the structure ans it's data, you need:- size_t GetTotalDataSize(){ size_t NrOfItems= sizeof(Arr)/sizeof(char*); size_t TotalSize=sizeof(Arr); for(int i=0;i

                    P Offline
                    P Offline
                    Peter Chan 0
                    wrote on last edited by
                    #9

                    Your description is very clear. I now know clearly how to use sizeof(). I still just have one question. Just as your sample.

                    char * Arr[]={"First Item","Second Item"};
                    

                    Do I need to delete Arr when it is useless. delete Arr[0]; delete Arr[1];

                    H 1 Reply Last reply
                    0
                    • P Peter Chan 0

                      Your description is very clear. I now know clearly how to use sizeof(). I still just have one question. Just as your sample.

                      char * Arr[]={"First Item","Second Item"};
                      

                      Do I need to delete Arr when it is useless. delete Arr[0]; delete Arr[1];

                      H Offline
                      H Offline
                      Hans Dietrich
                      wrote on last edited by
                      #10

                      Peter, Chan wrote:

                      Do I need to delete Arr when it is useless.

                      In this case, no. Since you did not allocate the strings using new, you do not have to free them using delete.

                      Best wishes, Hans


                      [CodeProject Forum Guidelines] [How To Ask A Question] [My Articles]

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups