ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
So maybe if I clear things up I wont confuse everyone.
I'm trying to pass two arrays to a function. Among other things
I want to compare the size of the array(# of elements)
which can be accomplished by
sizeof array / sizeof(int)
This works fine in main(), but once I reference the arrays
to a local function and I run the same code,
I get a value of 1.
Breaking it down,
sizeof array results in 4
sizeof(int) results in 4
4/4 results in 1
So, I guess in the local function, the first sizeof call
is working on the first element of the array and
not the whole array as in main( )
Does anyone know a trick for this, I am sure there must be.
When you pass the array to the reference variable, it becomes a pointer(sort of). So, when you say 'size of', you get the size of the reference variable(an int here) and not the size of the array.
As a generall guide, sizeof will only tell you the size of things whose size is known at compile time. As soon as you pass an array as an argument, as UltimaGuy said, it basically becomes a pointer, and (without complicated analysis of the code) the C compiler has no way of knowing the size of the block of memory a pointer is pointing to.
Alex
Last edited by llama_meme; 01-26-2004 at 06:19 AM.
smart pointers are a class available from www.boost.org
There are other implementations of smart pointers as well. The idea is that these pointers know about things like array bounds and can return the length of arrays of objects.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.