|
-
May 13th, 2003, 05:40 AM
#1
Thread Starter
Fanatic Member
#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 
-
May 13th, 2003, 07:56 AM
#2
Fanatic Member
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
-
May 13th, 2003, 11:00 AM
#3
Frenzied Member
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.
-
May 13th, 2003, 03:21 PM
#4
Fanatic Member
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
-
May 14th, 2003, 05:45 AM
#5
Thread Starter
Fanatic Member
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 
-
May 14th, 2003, 06:49 AM
#6
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.
-
May 15th, 2003, 05:16 AM
#7
Thread Starter
Fanatic Member
-
May 15th, 2003, 07:56 AM
#8
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.
-
May 15th, 2003, 09:27 AM
#9
Monday Morning Lunatic
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
-
May 15th, 2003, 09:46 AM
#10
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.
-
May 15th, 2003, 11:09 AM
#11
Monday Morning Lunatic
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
-
May 16th, 2003, 05:07 AM
#12
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.
-
May 16th, 2003, 09:16 AM
#13
Monday Morning Lunatic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|