Thread: function/operator size??

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    70

    function/operator size??

    I was wondering if there was some function/operator called size which determined the size of an array in c or c++

  2. #2
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172
    No.

    But there is one called 'sizeof()'. : )
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you have
    Code:
    int a[] = { 1, 0, 0, 2, 11, 99 };
    You get the answer 6 by doing
    sizeof(a) / sizeof(a[0] )

    This only works on real arrays.
    Pointers to arrays (either by function call parameter or allocated memory) do not work in this way.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    58
    is there any way of determining till what point in the array actually explicit insertion of elements has been made. i mean for example i have an array a[20]. now i ask user to input numbers into this array. if i dont keep track of the number of elements he has entered at this point is there any way of determining this at a later point of time.
    even a fish would not have been in danger had it kept its mouth shut.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Not unless you reserve some magic value (which the user cannot enter) and fill the array with that value before getting user input.
    It's much easier just to keep a count of these things
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Not from the a language perspective. However if you initalized the array with known values that can not be inputted then you can step through it yourself and count how many aren't of this value.

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    70
    what do you mean by
    "
    This only works on real arrays.
    Pointers to arrays (either by function call parameter or allocated memory) do not work in this way."

    gooddevil can you not use a linked list, you can keep count of the number of entries made by the user

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    @studentc
    Example:
    Code:
     char a[2];
    char *b = a;
    sizeof(a) would give 2 while sizeof(b) would give 4.
    Example 2:
    Code:
     char a[5][10]; /*A two dimensional array */
    char (*b)[10] = a;  /* A pointer to an array of 10 characters */
    Here sizeof(a) would be 50 and sizeof(b) would be 4. See what we mean now?

Popular pages Recent additions subscribe to a feed