Thread: arrays help please

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    3

    arrays help please

    should be a simple question
    i want to get the length of an int array
    i tried sizeof(arr) <- returns 4
    and i tried sizeof(arr)/sizeof(arr[0]) <- returns 1
    the length is 9
    when i do
    printf("%d", sizeof(arr)/sizeof(arr[0])) it prints 9
    but i want to do something like
    size = sizeof(arr)/sizeof(arr[0]) and it gives me 1
    what am i missing here ?

    thanks all helpers

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Question 6.21
    Read all the links.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    3
    ok i read it but nothing to point me to the answer..
    anyone?

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    sizeof pointer gives you sizeof the pointer. not sizeof what pointer is pointing to.
    Thus

    Code:
    int a[10];     // array
    int *p = a;   // pointer p points to a
    sizeof(a) ;   // gives sizeof(int) * 10 
    sizeof(p);   // gives sizeof(int*)
    When array is passed to function, you're only passing address of first element.
    ie. func( a ) is the same as func(&a[0]);

    You should have posted what is 'arr'!
    The best is to post minimal code that describes your problem.
    Last edited by Bayint Naung; 05-15-2011 at 09:50 AM.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    3
    arr is 1,2,3,...,9
    ok so by your example if i do
    insert
    Code:
     sizeof(a)/sizeof(p)
    it should give me the size ?
    it gives me 1 as the size

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Post the damn code!.
    Is arr declared as array or pointer?!
    sizeof(a)/sizeof(p)? perhaps you want sizeof(a) / sizeof(a[0])

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > printf("%d", sizeof(arr)/sizeof(arr[0])) it prints 9
    This only works when the array is IN SCOPE.

    Pointing to the array (even if it is in scope), or passing the array to another function (also making a pointer) means you end up applying sizeof to a pointer, and not to an array.
    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.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    > printf("%d", sizeof(arr)/sizeof(arr[0])) it prints 9
    This only works when the array is IN SCOPE.

    Pointing to the array (even if it is in scope), or passing the array to another function (also making a pointer) means you end up applying sizeof to a pointer, and not to an array.
    At the risk of upstaging my friend Salem...

    This happens because C does not actually know how big an array is, except in the scope where it's created. Outside that it's simply apointer to a block of memory that you, as a programmer, are required to treat correctly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  2. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  3. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  4. Parallel Arrays with Multiple Arrays
    By Billye Scott in forum C++ Programming
    Replies: 0
    Last Post: 03-02-2002, 11:14 PM
  5. separating line of arrays into array of arrays
    By robocop in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2001, 12:43 AM