any way to determine size of array thats a function parameter?

This is a discussion on any way to determine size of array thats a function parameter? within the C Programming forums, part of the General Programming Boards category; Hi, I have a function that accepts as its argument a pointer to a character array, and writes some values ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    1

    any way to determine size of array thats a function parameter?

    Hi, I have a function that accepts as its argument a pointer to a character array, and writes some values to it.

    i'd like to check the size of it (the number of elements) to make sure I don't overwrite past its boundaries

    Something like: sizeof(array)/sizeof(char) only works if the code is executed in main and the array is declared inside of main

    sizeof(array)/sizeof(char) does not work inside a function, because the compiler treats the passed in array as a pointer, and returns the size of the pointer, not the array.

    Any ideas?

    Note: I'm not using strlen because I want to check the actual size of the array, not the length of the string.
    Last edited by fishjie; 12-14-2004 at 04:23 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Pass the size as another parameter, [edit]and use sizeof(array)/sizeof(*array) for that parameter when array is in scope[/edit].
    Last edited by Dave_Sinkula; 12-14-2004 at 04:27 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    you could also use a struct or even pack the size into the array itself either behind or ahead of the pointer.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Well, in newer versions of C you can do this:
    Code:
    void myFunction( char str[n] )
    {
      printf("The array is %d bytes long.",n);
    }
    I haven't tested this (I don't program C), but from what I've heard, this is how it works.
    Last edited by Sang-drax; 12-14-2004 at 09:06 PM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Except...what is n?
    If you understand what you're doing, you're not learning anything.

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    It will contain the size of the array. Search for VLAs.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Sang-drax
    Well, in newer versions of C you can do this:
    Code:
    void myFunction( char str[n] )
    {
      printf("The array is %d bytes long.",n);
    }
    I haven't tested this (I don't program C), but from what I've heard, this is how it works.
    Hmmm. I believe you have to pass the parameter n, as in:
    Code:
    void myFunction( int n, char str[n] )
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    .
    Join Date
    Nov 2003
    Posts
    307
    Sang-Drax -

    VLA's are variable length arrays, part of C99 - they apply ONLY to arrays declared inside a function, and have scope only in that function.

    The scope of the string array passed in is in 'outside' the function.

    And, if you pass in the correct length of the array as a parameter, then you no longer have the problem of worrying about finding it.

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    OK, apparently I was misinformed.
    *runs back to the C++ board*
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  10. #10
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Quote Originally Posted by Dave_Sinkula
    Pass the size as another parameter, [edit]and use sizeof(array)/sizeof(*array) for that parameter when array is in scope[/edit].
    Please if you read this don't forget the If it is in scope

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    ^ Right, in other words saying:

    Code:
    int f(double *array) {
      size_t size = sizeof(array)/sizeof(*array);
      return (signed)size;
    }
    Is wrong as you are saying the size of a pointer divided by the size of a double. Which on my machine would be .5, which I guarantee you is not what you were looking for

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Which on my machine would be .5
    You mean 0, right?
    My best code is written with the delete key.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    >You mean 0, right?
    Nope I meant .5, but Prelude is correct in saying that division will evaluate the size of the array to zero. If I said 0 that means my brain also trucates decimals when doing integer math
    Last edited by master5001; 12-18-2004 at 01:45 AM.

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Actually...Prelude may have stumbled onto something here. From now on I will tell the IRS "Sorry my brain just truncated that decimal."

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by master5001
    >You mean 0, right?
    Nope I meant .5, but Prelude is correct in saying that sizeof() will evaluate the size of the array to zero. If I said 0 that means my brain also trucates decimals when doing integer math
    No. You mean 0. Integeral division will only ever give you an integer as a result. As such, it's impossible for that to give you .5.
    Code:
    int f(double *array) {
      size_t size = sizeof(array)/sizeof(*array);
      return (signed)size;
    }
    
    int main( void )
    {
        double array[] = { 0.0, 1.1, 2.2 };
        printf("returned value is %d\n", f( array ) );
        return 0;
    }
    How could you possibly get .5 on this? You can't. It's an impossiblility. There is no way your machine would give you .5 as a return value there. It doesn't have anything to do with the sizeof operator. It has to do with integer division.

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

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing multidimensional/dynamic array to function
    By epyfathom in forum C Programming
    Replies: 2
    Last Post: 04-02-2009, 05:39 PM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 02:55 AM
  3. dynamically defined array size in a function
    By earth_angel in forum C Programming
    Replies: 21
    Last Post: 05-28-2005, 01:44 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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