473,385 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

can sizeof( <array> ) work in a function body?

tekninja
I wanted to write a function to take the tedium out of dynamically determining the total number of elements in an array.

Within the main method this is easy as I simply use:
Expand|Select|Wrap|Line Numbers
  1. sizeof( <array name> ) / sizeof( <array type> )
However I am running into an annoying problem when trying to pass an array reference to a function that is suppossed to return the same value as the above would.

my function reads:
Expand|Select|Wrap|Line Numbers
  1. int numElements( int array[] ){
  2.     int sizeOfArray = sizeof( array ) / sizeof( int );
  3.     return sizeOfArray;
  4. }
and in the main body of my program the call looks like this (simplified to demonstrate):
Expand|Select|Wrap|Line Numbers
  1. int intArray[50];
  2. cout << numElements( intArray );
However this does not print 50, instead it keeps printing 1.

I am guessing this has something to do with the fact that an array name is really just a pointer to the first element in the array, and as a result the function is only seeing the first element (and therefore is returning 1)... but is there anyway to get the function to see the whole array?

I realize I could probably save myself the headache if I just used vectors... but I kind of wanted to see if I could get this to work with arrays.

So really all I want to do is condense
Expand|Select|Wrap|Line Numbers
  1. sizeof( <array name> ) / sizeof( <array type> )
into a reusable function.

Thanks for any help!
Aug 11 '07 #1
1 1623
weaknessforcats
9,208 Expert Mod 8TB
Nope.

Use vectors.

sizeof( a thing) reports the size of the thing. In the case of an array this is the address of element 0, which is a pointer, so you get the size of the pointer.

sizeof type reports the sizof the type. In this case an int.

So you now have 4 / 4 and your array is 1 element. Oops.

Arrays in C++ are for very low level work. vector is required to be implemented as an array so all you would be doing with your function is duplicating code already done for you in vector.
Aug 11 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Karl Ebener | last post by:
Hi! I asked a similar question before but then changed everything to using char-Arrays instead of the string class, but I would rather not do this again. So, does anyone know of a...
3
by: Siemel Naran | last post by:
Here is a question about implicit conversion from T (&) to ptrcarray<T>. I wrote a class template <class T> struct ptrcarray { T * array; size_t size;
5
by: pmatos | last post by:
Hi all, I have a vector of vector of ints, I could use C approach by using int but I think C++ vector<vector<int> > would be easier to manage. So I have a function which creates and initializes...
16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
2
by: ANaiveProgrammer | last post by:
Hi all I have been trying to send a struct over socket. I tried to assign the values of struct to unsigned char array. I have attached both .c files at the bottom. 'Client.c' takes a struct,...
9
by: Simple Simon | last post by:
Java longs are 8 bytes. I have a Java long that is coming in from the network, and that represents milliseconds since Epoch (Jan 1 1970 00:00:00). I'm having trouble understanding how to get it...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
10
by: arnuld | last post by:
WANTED: /* C++ Primer - 4/e * * Exercise: 9.26 * STATEMENT * Using the following definition of ia, copy ia into a vector and into a list. Use the single iterator form of erase to...
43
by: john | last post by:
Hi, in TC++PL 3 on pages 674-675 it is mentioned: "Maybe your first idea for a two-dimensional vector was something like this: class Matrix { valarray< valarray<doublev; public: // ... };
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.