how to know size of integer array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kujahleague
    New Member
    • Jul 2006
    • 11

    how to know size of integer array?

    Been bothering me so long, we can find the size of string array (array of char) but what is the way to find the size for integer array? I've tried using while loop until it find '\0' null character, it's stop whenever integer 0 is encountered, which is not what I want .. anyone please help me
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You find the size of an integer array in the same way that you find the size of a char array or the size of any array, using the sizeof operator

    So for any type T

    Code:
    T array[<SomeSize>];
    
    size_t size_of_array = (sizeof array)/(sizeof array[0]);
    Because C guarentees that sizeof char = 1 for char arrays this simplifies to

    Code:
    char array[<SomeSize>];
    
    size_t size_of_array = sizeof array;
    I think you are confusing the size of a char array with the length of the string it contains

    Code:
    char string[50] = "Hello World!";
    
    if (strlen(string)+1 == sizeof string)
    {
        printf("String is exactly the right size for '%s'\n", string );
    }
    else if (strlen(string)+1 < sizeof string)
    {
        printf("String is bigger than required to hold '%s'\n", string );
    }
    else
    {
        printf("String is too small. DANGER Undefined Behaviour Envoked\n");
    }

    Comment

    • kujahleague
      New Member
      • Jul 2006
      • 11

      #3
      thank you for your answer, but what I need to find is the size of array (length of array of integer) maybe I said it not clear enough, sorry

      I've used sizeof [n] but it returns me same value each time (4) I don't know why
      where n is array of integer, which I assign numbers of integer into it

      please help

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        I have given you the correct solution in my previous post. what you have just posted does not make sense without you specifying what [n] is.

        If what you say is true for

        int array[5];

        sizeof(array) == 4

        then there is a bug in your compiler.

        I suggest however that you post the actual code you are using as it is more likely that there is a different error, for instance is it really and array or is it a pointer to an int to which you are assigning memory for an array of integers.

        Comment

        • kujahleague
          New Member
          • Jul 2006
          • 11

          #5
          Code:
          int count(int thearray[]){
              int count, i;
              i=0;
              
              count=0;
              printf("%d\n", count);
              while (thearray[i]!='\0'){
                    count++;
                    i++;
              }
              printf("%d\n", count);
              return count;
          }
          
          int Max(int thearray[])
          {
              int max = thearray[0];    // first element be the max
          //int length = sizeof(thearray);
          
              int length = count(thearray);
              int i;
              
              printf("length is %d\n", length);
              for(i = 1 ; i<length ;i++)
              {
                  if (thearray[i] >  max )
                  {
                      // if the elemnt at index   is greater than
                      //previous max , than set this value to the max
                      max = thearray[i] ;
                  } //end of if 
          
              }//end of for 
          printf("Max is %d\n", max);
              return max;
          }
          
          int main(){
          //int thearray[100];
          int max;
          
          int thearray[] = {1,3,23,343,23,1212,23287,84547};
          max=0;
          printf("Max is %d\n", max);
          max = Max(thearray);
          printf("Max is %d\n", max);
          system("PAUSE");	
              return 0;
          }
          My code so far it doesn't work right both way (using count function or sizeof array)

          Hope there is no bug in my compiler though

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            In either of the prototypes

            int count(int thearray[])
            int Max(int thearray[])

            thearray is declared as a pointer, you are probably getting confused because you are using the [] notation but this does not declare an array this is just another way of writting

            int Max(int *thearray)

            and a fairly confusing one as you have found out. sizeof(int *) == 4 on a lot of systems (and so it seems on yours).

            You can not find the size of an array from a pointer to it, it is impossible, you have to have access to the array it self to find it's size. This means that if you are goinf to pass an array by passing a pointer to it (a normal thing to do) then you need to pass the size of the array as well. The one acception to this is char array where it is being use to store a string because you can use the NULL terminator '\0' to locate the end of the string (but not the size of the array).

            What you should do is change your prototype of Max to

            int Max(int *thearray, int number_of_ints)

            and in main (where thearray is defined) call Max as

            max = Max(thearray, (sizeof thearray)/(sizeof thearry[0]));

            This will work because you are using the sizeof opertor in the same context that the array is declared in.

            Comment

            • amit kumar
              New Member
              • Aug 2006
              • 1

              #7
              Originally posted by kujahleague
              Been bothering me so long, we can find the size of string array (array of char) but what is the way to find the size for integer array? I've tried using while loop until it find '\0' null character, it's stop whenever integer 0 is encountered, which is not what I want .. anyone please help me


              u can use sizeof operator to know about the sizeof array in terms of no of bytes then divide it by size of integer ,u will get the no of elements in the array

              Comment

              • kujahleague
                New Member
                • Jul 2006
                • 11

                #8
                Thank you for all answers, I'll try

                Comment

                • SatishAradhya
                  New Member
                  • Aug 2006
                  • 2

                  #9
                  you can try this code

                  int arr[10];
                  int x;
                  x=sizeof(arr)
                  printf("%d",x/2);

                  :cool:

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    Originally posted by SatishAradhya
                    you can try this code

                    int arr[10];
                    int x;
                    x=sizeof(arr)
                    printf("%d",x/2);
                    This code makes an assumtion about the size of an integer and it wont give the right answer if sizeof int != 2

                    Comment

                    • shinelakshmanan
                      New Member
                      • Aug 2006
                      • 13

                      #11
                      Originally posted by kujahleague
                      thank you for your answer, but what I need to find is the size of array (length of array of integer) maybe I said it not clear enough, sorry

                      I've used sizeof [n] but it returns me same value each time (4) I don't know why
                      where n is array of integer, which I assign numbers of integer into it

                      please help
                      If n is an integer,
                      sizeof(n) will always give 4,
                      but if n is an array of integers,
                      sizeof(n) will give (4*number of elements in n).

                      Comment

                      • ssehgal2010
                        New Member
                        • Aug 2006
                        • 10

                        #12
                        Hi

                        Getting integer array size is prettry simple
                        Lets say I have int array i[5]

                        then
                        sizeof(i)/sizeof(i[0]) will give me 5 i.e. the length of integer array

                        Comment

                        • DutchUncle

                          #13
                          I see a miscommunicatio n. If the array is declared with an explicit size, as in a[5], the compiler knows it is an array and knows how big it is, so sizeof() returns the total size of the array. If the array is incompletely declared, as in a[] (commonly used for a parameter or a const-initialized array), then the compiler only knows that "a" is a POINTER, not the array itself, and the sizeof refers to the POINTER.

                          Comment

                          • Bugdog
                            New Member
                            • Aug 2011
                            • 1

                            #14
                            Are you sure? I do understand that it may depend on standards and how they are implemented by compilers, but when I declare an array like this:

                            const int arr[] = { 1,2,3,4,5,6,7,8 ,9,10 };

                            The sizeof(arr) gives me 4 * 10, provided that sizeof(int) in my system is 4.

                            Comment

                            • Banfa
                              Recognized Expert Expert
                              • Feb 2006
                              • 9067

                              #15
                              @Bugdog you are correct, when you declare an array without specifying a size, i.e. when you actually are creating an array then the sizeof operator will give to correct size because the name actually is the name of an array.

                              when you using the [] notation in a function parameter list it is exactly the same as declaring a pointer (which can be treated very similarly to arrays). The name is a name of a pointer and not the name of an array and you get the size of the pointer if you use sizeof.

                              Comment

                              Working...