Using "sizeof" to get size of array

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Endar
Posts: 145
Joined: Mon Jun 14, 2004 7:59 am

Using "sizeof" to get size of array

Post by Endar »

Hey guys

I'm using the sizeof operator to get the size of an array. So: (sizeof array / sizeof array[0] )

But because I have a class with a whole lot of functions, this is where I have a function that does this, which brings me to my problem.

When passed to a function as a pointer, as you do with arrays, it takes array as a pointer to the first element in the array, so the result of the expression is always 1, instead of 17, as it should be :D.
"The reputation of a thousand years may be determined by the conduct of one hour."
-Japanese Proverb
jox
Bug Slayer
Posts: 726
Joined: Thu Apr 22, 2004 6:55 pm
Location: Germany

Post by jox »

Maybe pass the size of the array as an additional parameter?
Electron
Posts: 874
Joined: Sun Mar 14, 2004 12:05 am
Location: Massachusetts USA

Post by Electron »

you could use irrlicht arrays which have a size() method
You do a lot of programming? Really? I try to get some in, but the debugging keeps me pretty busy.

Crucible of Stars
Endar
Posts: 145
Joined: Mon Jun 14, 2004 7:59 am

Post by Endar »

jox: that's actually what I'm doing. I've got a class that just has a whole lot of sorting functions in it. And I'm trying to find a way to call the functions like this: sort.quickSort( array, sort.sizeOfArray(array) );

Electron: This isn't actually for irrlicht specifically, so I'm not using the irrlicht types.
"The reputation of a thousand years may be determined by the conduct of one hour."
-Japanese Proverb
Alex
Posts: 8
Joined: Fri Jul 02, 2004 5:27 am
Location: Western Australia
Contact:

Post by Alex »

Why use arrays at all when std::list and so on have sort methods built in?

Just a thought.
thesmileman
Posts: 360
Joined: Tue Feb 10, 2004 2:20 am
Location: Lubbock, TX

Post by thesmileman »

I would suggest std::vector as it is an array that is dynamically resized as needed. It has the size feature and a sort feature that will be much faster than something you write yourself unless you are really good but even then the difference will probibly be quite small.

But to answer your question as you ask it: dereference the pointer to the array in your call to the sizeof function.

Also if this is not irrlicht specific shouldn't it be posted in one of the other sections?
joy
Posts: 124
Joined: Tue Apr 27, 2004 9:15 am
Location: Germany

Post by joy »

What about passing &array ? would that help?
Post Reply