how do I find the length of and array using sizeof()

This is a discussion on how do I find the length of and array using sizeof() within the C Programming forums, part of the General Programming Boards category; Damm I forgot this I knew this a week ago but it slipped out. Code: #include<stdio.h> main() { int x; ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    10

    how do I find the length of and array using sizeof()

    Damm I forgot this I knew this a week ago but it slipped out.

    Code:
    #include<stdio.h>
    
    main() 
    {
           int x;
           char array[100][50] =
           {
                {"element 1"},
                {"element 2"},
                {"element 3"},
                {"element 4"}
           };
            
           x = sizeof(array)/sizeof(array[0]);
           printf("the size of the array is %d",x);
           getchar();
    }
    I am not getting the right results it should be 4 not 100 I forgot this I knew this a week ago. I guess the old saying is true in programming if you don't use it you lose it

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It shouldn't be 4. You have declared a 100 x 50 array, it has all those elements, you have only initialized a few of them. The rest initialize to zero.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    10

    shortcut notation

    is there a short cut notation in c to initialize the rest of the elements to the null character?

    Thanks for the reply by the way I really appreciate it

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you initialize any elements of an array, all uninitialized elements are initialized to zero by default.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    10
    char array[100] = {0};

    you use this short hand notation to explicitly set the first element to zero and implicitly set all the other elements to zero
    when you first initialize an array


    I just had to get that into in my head

    Code:
         int i;
      
          char array[100] = {0}; // explicitly set the first element to zero and implicitly set all the other elements to zero 
    
          array[0] = 1;
          array[1] = 2;
          
          for(i =0; array[i] != 0; i++)
          {
          }      
          
          printf("the size of the array is %i",i);
    thanks all later gents I like this forum by the way.

  6. #6
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    This does only work if the actual content of an array element isn't 0, which is absolutely possible.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    As KONI said, That's not going to give you the size of the array...

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    I dont really think that there is any other way of finding a length other than traversing the array through a loop.

    May be any one have better idea?

    ssharish2005
    Last edited by ssharish2005; 04-27-2007 at 03:39 AM.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It doesn't look like they really want the array's size. It looks like they just want to know how many in a row from the start aren't zero filled.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I suppose you could have a struct where you would have a pointer, representing your array and a regular int to hold the size. There are other variations on this. Of course, the important thing is to make sure that the size variable is altered everytime the size of the array changes.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21