Results 1 to 13 of 13

Thread: #elements in array in functions

  1. #1

    Thread Starter
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking #elements in array in functions

    Firstly, i know that in normal "int main(void)" stuff, to find the # of elements in an array, say int p[], you do:
    num_elem=sizeof(p)/sizeof(int)
    (**i think sizeof(p)/sizeof(p[0]) is more general**)

    However, i was passing an array to a function:

    int my_func(int q[])
    {
    cout<<sizeof(q)<<endl; //Gives 4 (=sizeof(int)) every time
    }

    I was just wondering if there was a nice way to find the size of an array passed to a function??

    Thanks
    sql_lall

  2. #2
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Why not just pass the number of elements in the array to the function, that way you don't have to do callculations on unused places in the array. For example
    Code:
    int array[30];
    
    for (int i = 0; i < 10; ++i)
    {
    	array[i] = i + 1;
    }
    Now if you do sizeof(array)/sizeof(array[0]) you would get 30, but we've only actually only used 10 spots... If we pass the number of elements to the funcion this is not going to be a problem...
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  3. #3
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Pass the number of elements in the array. Doing what you are trying to do is a bad idea, and will only occasionally and under specific circumstances, work.

    Z.

  4. #4
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Originally posted by Zaei
    Pass the number of elements in the array. Doing what you are trying to do is a bad idea, and will only occasionally and under specific circumstances, work.

    Z.
    Yeah, that's pretty much what I was trying to say
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  5. #5

    Thread Starter
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking Ok

    Yeah, i get what ur saying, and it would normally work. However, i forgot to add that this is for a competition, and all they do is call your function (actually part of a class) with the parameters as an array.
    I.e. you write an class, with a function called "X(int p[])" or whatever. To mark you, they pass and array to X, and test the returned value. Hence, as they do not pass the #elements when testing, the suggestions don't work for this case.
    Thanks anyway, all suggestions appreciated
    sql_lall

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    What are they passing in the array? There is no way to find out the number of elements in C, it's always your responsibility to keep track of that.

    For that matter, what's the wording of the competition problem? Maybe there's a clue.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking hehe

    Nah, there's no way to pass the size, all you can do is write a function that accepts an array, thats it.
    BTW, i'm using C++, and hoping there is *some* way to do it...
    sql_lall

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Would you fulfill the specification if you wrote an array class that stores the size along with the array?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Like a vector or a Boost.Array?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    More like boost::array.
    Though isn't boost::array constant size through templates?
    I was thinking more along the lines of constant size through constructor parameter.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I was doing it their style as Boost.Package not boost:ackage

    A vector can be "constant size" through a parameter, and for all intents and purposes is identical speed to a raw array.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It wasn't like "more like boost::array than Boost.Array" but rather like "more like boost::array than std::vector".
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Oh.

    /me beats head on desk

    I see what you're getting at now
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width